PAT b 1070 Ropes (c + + edition)

Source: Internet
Author: User

1070. Ropes (25) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

Given a piece of rope, you need to string them into a rope. Each time the concatenation is done, the two pieces of rope are folded in half, and then set together as shown. The resulting rope is again treated as another piece of rope, and can be folded in half to another string. After each concatenation, the length of the original two-segment rope will be halved.

Given the length of the N-segment rope, you need to find out the maximum length of the rope they can string into.

Input format:

Each input consists of 1 test cases. The 1th line of each test case gives a positive integer n (2 <= n <= 104), and the 2nd line gives n positive integers, that is, the length of the original rope segment, separated by a space between the numbers. All integers are less than 104.

Output format:

Outputs the maximum length of a string that can be strung in a row. The result is rounded down, which is the nearest integer that does not exceed the maximum length.

Input Sample:
810 15 12 3 4 13 1 15
Sample output:
14


Idea: The problem requires the maximum length, so we need to know why the same half of the operation will lead to different results, the reason is that the large number of half and the small number of the result is different, the large number is much less, so as far as possible to reduce the number of the number of half of the times, such as 2 5 10, certainly according to 10 again 2 the smallest so that's why use sort of reason

1 //1070.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <typeinfo>7#include <algorithm>8 9 using namespacestd;Ten  One intMain () A { -     intn,i; -  theCIN >>N; -  -     Double*p =New Double[n],sum=0; -  +      for(i =0; i < N; i++) -CIN >>P[i]; +  ASort (p, p +N); at  -      for(i =0; I < n1; i++) -P[i +1] = (P[i] + p[i +1]) /2.0; -  -cout << static_cast<int> (P[i]) <<Endl; -  in     Delete[] p; -  to     return 0; +}

PAT b 1070 Ropes (c + + edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.