PAT (Advanced Level) 1125. Chain the Ropes (25)

来源:互联网 发布:咸阳办公软件电脑培训 编辑:程序博客网 时间:2024/06/11 03:26

1125. Chain the Ropes (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

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 <= 104). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 104.

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:
810 15 12 3 4 13 1 15
Sample Output:

14

说实话,刚看到这个题目,我个人感觉是很吓人的,因为,,很多英文单词看不懂?

比如:Each time you may only fold two segments into loops and chain them into one piece这句,首先,Each time我就理解错了,这不是说只能处理一次,而是说,在一次测试用例里面,我们可以多次的去将其中两段绳子折叠(折叠:简单来说,就是两段绳子长度之和除以2啦~所以要用double来承载结果,所以考场上一直写int是错误的)然后是:After each chaining, the lengths of the original two segments will be halved这句,halved这个单词不认识,鉴于我的英语水平,我就猜测是和的意思,其实halved这个单词的意思是“减半”~(就是上面说的要除以2的意思)最后是这一句:The result must be rounded to the nearest integer that is no greater than the maximum length.我真的纠结了好久,后来百度翻译,意思就是最后得到的那个double要给它近似到一个int(它是不能超过最大长度的一个值),其实很显然了~

思路就是:先将所有原始数据排序(sort),然后从最小的两个数开始求和除以2往前覆盖,最后读出a[0]就好了,后来有3个测试点死都过不去,问了大神才知道原来是数组开小了(以前一直以为开小只会报段错误啊:P)

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 3e5 + 10;int n;double a[maxn];bool cmp(double a, double b) {return a > b;}int main() {double ans = 0.0;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%lf", &a[i]);}sort(a, a + n, cmp);for (int i = n - 2; i >= 0; i--) {ans = (a[i] + a[i + 1]) / 2;a[i] = ans;if (i == 0) {printf("%d\n", (int)(a[0]));}}return 0;}


0 0