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 ...
随机推荐
- 2015 Changchun Regional
弱没机会去长春,但拿了题来做了,加上请教各路大牛,理论AC了一发,但没实现~(感谢各路有形无形的大牛的指导) A题~Too Rich 给你1,5,10,20,50,100,200,500,1000,2 ...
- leetcode 二分法 Pow(x, n)
Pow(x, n) Total Accepted: 25273 Total Submissions: 97470My Submissions Implement pow(x, n). 题意:求x的n次 ...
- php debug二三事
最近php相关项目遇到了一些问题,处理起来让人感觉挺有意思,寥寥记下. 1.php执行后常驻内存里,需要kill掉该进程再重启,才能让修改后的代码生效. 之前有一次组里小伙伴修改了一个长期后台进程运行 ...
- 【 D3.js 进阶系列 — 2.2 】 力学图的參数
力学图的布局中有非常多參数.本文将逐个说明. D3 中的力学图布局是使用韦尔莱积分法计算的.这是一种用于求解牛顿运动方程的数值方法,被广泛应用于分子动力学模拟以及视频游戏中. 定义布局的代码例如以下: ...
- JQuery实现复制到剪贴板功能
在网页中实现复制到剪贴板功能,有两种方法, 第1种方法:使用JavaScript自带的方法,但是这种方法只能在IE下使用. document.execCommand("Copy") ...
- Codeforces Round #330 (Div. 2) D. Max and Bike 二分
D. Max and Bike For months Maxim has been coming to work on his favorite bicycle. And quite recently ...
- write data to xml
public class Student { public int Id { get; set; } public string FirstName { get; set; } public stri ...
- 判断文件是否为空 C++
#include <sys/stat.h> int stat(const char *restrict pathname, struct stat *restrict buf); stru ...
- 使用Web Workers处理线程
使用HTML 4和JavaScript创建出来的Web程序中,因为所有的处理都是在单线程中 HTML 5的Web Workers API,HTML 5中,一个Worker实际上为一个后台运行的线程.
- MySQL优化小方法
一.查询优化 1.尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而 ...
