PAT1125:Chain the Ropes
1125. Chain the Ropes (25)
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 思路 可以用哈夫曼树的思路(其实本质就是一个贪婪算法):优先选两个最小的绳子合成新的绳子,然后循环即可。
所以为了得到最长的绳子,那么只需要对输入数据升序排下序即可。 代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
vector<int> ropes(N);
for(int i = ;i < N;i++)
{
cin >> ropes[i];
}
sort(ropes.begin(),ropes.end());
int length = ;
for(int i = ;i < N ;i++)
{
if(i == )
{
length = ropes[i];
continue;
}
length = (length + ropes[i])/;
}
cout << length << endl;
}
}
PAT1125:Chain the Ropes的更多相关文章
- 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 ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- 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 ...
随机推荐
- RHEL6从源码安装python及其他软件包
RHEL6从源码安装python及其他软件包 ## install ssl $ sudo yum install openssl-devel or: $ sudo apt-get install li ...
- Java 继承Thread类和实现Runnable接口的区别
ava中线程的创建有两种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 在实际应用中,我 ...
- web.xml 详细介绍
url:http://mianhuaman.iteye.com/blog/1105522#bc2344393 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<l ...
- ERP-非财务人员的财务培训教(二)------如何评价公司/部门经营业绩
一.财务比率分析 第一节 流动性比率 第二节 经营比率 第三节 资本结构比率 第四节 获利能力比率 第五节 现金流量比率 第六节 获现能力比率 二.财务比率金字塔 第二部分 如何评价公 ...
- Unity 数据Json格式的转换
把对象转换为字节序列的过程称为对象的序列化. 把字节序列化恢复为对象过程称为对象的反序列化. JSON格式的转换,是一大神给我说的,让我拿来存储数据库时对一些数据的处理,感觉特别好用.但是我并没有深入 ...
- unity xml序列化与反序列化 多平台
换平台确实是一个头疼的问题,本来在pc用.net的json处理数据很是顺手的,但是发布web版本后,发现他不支持.后面找了好几个开源json都不能很好的支持web,或者不能支持List等.于是我就想着 ...
- rails常用命令备忘
rails new xxx 创建一个新rails项目 rails generate scaffold xxx 创建表模型,视图,控制器和迁移的"脚手架" rake db:migra ...
- 如何在centos操作系统上发布.net core的项目
环境:操作系统: centos 7.net core: 2.1.101 官方网站的示例地址: https://docs.microsoft.com/zh-cn/dotnet/core/linux-pr ...
- orderBy新写法
通常,我们处理排序规则的处理方法是在sql 语句中order by create_time desc, 但是这时我们需要从控制器中一步步找到该方法,操作多. 我们试着将业务逻辑拆分到控制器 中, 把排 ...
- Python函数式实现单例特性
传统的单例一般是基于类的特性实现,Python模块是天生的单例,下面来个简单的借助模块和函数实现单例特性: gdb = None def get_gdb(): global gdb if gdb is ...