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 15Sample Output:
14
- pat-aChain the Ropes (25)
- PAT 1125. Chain the Ropes (25)-甲级
- PAT 1125. Chain the Ropes (25)
- PAT A1125. Chain the Ropes (25)(哈夫曼树)
- PAT甲级.1125. Chain the Ropes (25)
- PAT甲级 1125. Chain the Ropes (25)
- PAT 甲级 1125. Chain the Ropes (25)
- PAT--1125. Chain the Ropes
- PAT A1125 Chain the Ropes
- PAT (Advanced Level) Practise 1125 Chain the Ropes (25)
- PAT (Advanced Level) 1125. Chain the Ropes (25)
- PAT (Advanced Level) 1125. Chain the Ropes (25) 解题报告
- PAT (Advanced Level) Practise 1125 Chain the Ropes (25)
- 【PAT】【Advanced Level】1125. Chain the Ropes (25)
- pat 甲1125. Chain the Ropes (贪心)
- 1125. Chain the Ropes (25) PAT 甲级&&1070. 结绳(25) PAT 乙级
- 1125. Chain the Ropes (25)
- 1125. Chain the Ropes (25)
- Android的日志工具Log
- KMP算法
- 三十天
- 有关散列表
- 多校4 HDU6069
- pat-aChain the Ropes (25)
- 单向链表的一些基础接口的实现
- 数论(基础欧拉函数)
- JSP学习笔记2
- python中的range与list函数
- effect java 学习摘要(4)
- elasticsearch的2.x 和5.x的不同之处之query
- Excel在统计分析中的应用—第四章—数据库统计函数与数据透视表-Part2-(数据库统计函数)
- 洛谷P1197 星球大战