pat-aChain the Ropes (25)

来源:互联网 发布:linux执行命令的过程 编辑:程序博客网 时间:2024/06/11 15:22

每次选择两根绳子合在一起,每合一次总长度就是两端和的一半。只要让长的尽可能晚的合并就行了。实质就是一个哈夫曼树。

输出那里不是四舍五入,是向下取整,不然最后一个点过不了

#include<cstdio>#include<queue>#include<functional>using namespace std;struct node{double d;node(double a=0):d(a){}friend bool operator < (const node& m,const node& n){return m.d>n.d;}};int main(){int n;double t;priority_queue<node> q;scanf("%d",&n);for(int i=0;i<n;++i){scanf("%lf",&t);q.push(node(t));}while(q.size()!=1){node te=q.top();q.pop();node t2=q.top();q.pop();double a=(te.d+t2.d)/2.0;q.push({a});}double ans=q.top().d;int an=(int)ans;printf("%d\n",an);} 

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

原创粉丝点击