栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

A1125 Chain the Ropes(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【排序】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A1125 Chain the Ropes(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【排序】

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:
8
10 15 12 3 4 13 1 15

Sample Output:
14
题意分析:

题目要求是要求所有绳索连接起来的最大长度,注意每次进行两个绳索连接操作的话,该两条绳索的长度之和就要除以2,并舍去小数,所以如果要使最后的和最大,那么就要让最长的绳索做的折半次数最少,故在获取所有绳索后先做一个从小到大的排序处理,从小到大依次折半,越小的折半后损失的长度最小。

代码如下:
#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector vec(n);
	for (int i = 0; i < n; i++) {
		cin >> vec[i];
	}
	sort(vec.begin(), vec.end());
	int index = 0;
	while (index!=n-1) {
		int temp = (vec[index] + vec[index + 1]) / 2;
		index++;
		vec[index] = temp;
	}
	cout << vec[index] << endl;
	return 0;
}
运行结果如下: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026920.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号