PAT_A1125#Chain the Ropes
Source:
Description:
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). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 1.
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
Keys:
- 哈夫曼树
- 贪心
Attention:
- 依次把最小的结点相加即可
- a1 < a2 < a3, 则(a1+a2)/2 < a3
Code:
/*
Data: 2019-08-13 19:44:48
Problem: PAT_A1125#Chain the Ropes
AC: 14:23 题目大意:
两段绳子结在一起后长度减半
输入:
给出N段绳子及其长度
输出:
最大长度
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e4+; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,r[M];
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &r[i]);
sort(r,r+n);
for(int i=; i<n; i++)
r[] = (r[]+r[i])/;
printf("%d", r[]); return ;
}
PAT_A1125#Chain the Ropes的更多相关文章
- PAT1125:Chain the Ropes
1125. Chain the Ropes (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 1125 Chain the Ropes (25 分)
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- PAT甲级 1125. Chain the Ropes (25)
1125. Chain the Ropes (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- PAT 1125 Chain the Ropes[一般]
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- A1125. Chain the Ropes
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...
- PAT 1125 Chain the Ropes
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...
- 1125. Chain the Ropes (25)
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...
- PAT甲级——A1125 Chain the Ropes【25】
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...
- PAT甲题题解-1125. Chain the Ropes (25)-贪心水题
贪心水题,每次取最短的两个绳子合并,长度缩减成一半 #include <iostream> #include <cstdio> #include <algorithm&g ...
随机推荐
- BZOJ——T 4563: [Haoi2016]放棋子
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 387 Solved: 247[Submit][Status][Discuss] Descriptio ...
- EasyUI 在textbox里面输入数据敲回车后查询和普通在textbox输入数据敲回车的区别
EasyUI实现回车键触发事件 $('#id').textbox('textbox').keydown(function (e) { if (e.keyCode == 13) { alert('ent ...
- springmvc 中开发Server Send Event
springmvc 中开发Server Send Event 学习了:http://blog.csdn.net/leiliz/article/details/55195203 https://www. ...
- 整理100道 .net面试题
前段时间,我在准备面试的时搜到的一套 net开发人员面试题,感觉比较全面,一直保存在草稿,刚在整理后台时翻了出来,干脆就发出来好了,以备不时之需. 1. .NET和C#有什么区别 答:.NET一般指 ...
- 民意调查Django实现(三)
我们接着第二小节的開始继续我们的旅程. 我们会继续Web-poll应用.而且将会专注于创建公共接口 - "Views". 哲学思想 一个视图是你的Django应用中的一个Web页面 ...
- ambarella H2 添加文件到ext4文件系统
方法1: ambarella/rootfs目录下有skeleton(骨架)目录,此目录下就是文件系统的各个目录, [root@jz4775dev]# ls skeleton/ bin debug de ...
- 君正Ingenic X1000E_halley2 更改Logo
有两种方法可以改变开机logo,编译进内核或者修改u-boot. <一>.编译进内核 一. 制作LOGO图片(可以使用gimp) 1. 制作一个.ppm格式图片(logo_tvu_clut ...
- 0423-mysql查询语句大全
建表.数据插入代码: #新建学生表 drop table if exists student; create table student( sno ) not null primary key com ...
- hihoCoder 1187
今天BC爆0了....但是日子还是要过的....要回学校毕业了~~大学就这么“荒废”了. 这个是hihoCoder的1187,比较基础的一道题. 题目链接: http://hihocoder.com/ ...
- java 中接口的概念
接口接口在java中是一个抽象的类型,是抽象方法的集合,接口通常使用interface来声明,一个类通过继承接口的方式从而继承接口的抽象方法.接口并不是类,编写接口的方式和类的很相似,但是他们属于不同 ...
