PAT B exam - rope

Given a section of the rope, you need to put them strung a rope. Each time series, the two ropes is folded telescoped together again as shown in FIG. The rope thus obtained has been treated as another piece of rope, you can talk to another piece of rope in series folded again. After each series, two of the original length of the rope is cut in half.
Here Insert Picture Description
N given length of rope segment, you need to find the maximum length they strung a rope.

Input formats:

Each input comprises a test. Each test case is given row of the first positive integer N (≦ n ≦ 10 . 4 );
line 2 gives the positive integer N, i.e. separated by a space between the original length of the cord segments, digital. All integer not exceed 10 4 .

Output formats:

The maximum length of the output line can be strung rope. The result is rounded down to the nearest integer that is taken not to exceed the maximum length.

Sample input:

8
10 15 12 3 4 13 1 15

Sample output:

14

Thinking

Essentially two numbers together and then / 2 plus a new number, and so forth.
Want to get the maximum length, it is necessary to start small fold, guarantee a minimum length loss.

Two ideas:

1. The length of the sort of the input, and then successively calculates the time o (nlogn)
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	int lens[n];
	for(int i=0; i<n; i++)
		scanf("%d",lens+i);
	sort(lens,lens+n);
	int sum=lens[0];
	for(int i=1; i<n; i++)
		sum=(sum+lens[i])/2;
	printf("%d",sum);
	return 0;
}
(R) 2. Statistics by counting the length of time o (n)

Due to the rope came from small to large, of course, is sorting method first thought. However, the length of the rope is less than 10 ^ 4, since it should be the case with most int [10000] array, it is better to use it skillfully quantity rather than the length of the recording, so that no sort, time complexity is O (N ).
The shortest rope and does not require the shortest rope before averaging, but if you set the initial value for the shortest length of rope, do not have this special treatment, the first for loop is to do this.
Look out, but for the test of mathematical abilities title with 25 points, is the indirect study of data structures and algorithms.
Author: OliverLew
link: https: //www.jianshu.com/p/ec4ccf1a76c8
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

#include <stdio.h>
int main()
{
    int l[10001] = {0}, N, i;
    double length = 0;

    scanf("%d", &N);
    for(int j = 0; j < N; j++)
    {
        scanf("%d", &i);
        l[i]++;                 /* record counts */
    }

    for(i = 0; i < 10001; i++)  /* find the shortest, special case */
        if(l[i])
        {
            length = i;
            break;
        }

    for(; i < 10001; i++)       /* make new ropes */
        while(l[i]--)
            length = (length + i) / 2;
    printf("%d", (int)length);
    return 0;
}
Published 35 original articles · won praise 2 · Views 895

Guess you like

Origin blog.csdn.net/qq_45735810/article/details/104222254