A1125 Chain the Ropes (25 point(s))

贪心--取最大长度

1. 原文

Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold two segments into loops and chain them into one piece, as shown by the figure. The resulting chain will be treated as another segment of rope and can be folded again. After each chaining, the lengths of the original two segments will be halved.

rope.jpg

Your job is to make the longest possible rope out of N given segments.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (2≤N≤$10^4$). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than $10^4$.

output Specification:

For each case, print in a line the length of the longest possible rope that can be made by the given segments. The result must be rounded to the nearest integer that is no greater than the maximum length.

Sample Input:

1
2
8
10 15 12 3 4 13 1 15

Sample output:

1
14

2. 解析

绳对折,加一段绳再对折,… 使长度最大

绳索长度从小到大排序,长度最小的持续被对折,最长的就仅被对折一次

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<cstdio>
#include<algorithm>
using namespace std;
int num[10010]={};
int main()
{
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
scanf("%d",&num[i]);
}
sort(num,num+n);
double ans=1.0*num[0];
for (int i = 1; i < n; ++i)
{
ans+=1.0*num[i];
ans/=2;
}
printf("%d\n", (int)ans);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1125/
  • 发布时间: 2019-09-03 08:34
  • 更新时间: 2021-10-29 14:09
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!