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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,s[],d;
int main()
{
cin>>n;
for(int i = ;i < n;i ++)
{
cin>>s[i];
}
sort(s,s + n);
for(int i = ;i < n - ;i ++)
{
d = (s[i] + s[i + ]) / ;
for(int j = i + ;j < n;j ++)
{
if(s[j] < d)s[j - ] = s[j];
else
{
s[j - ] = d;
break;
}
}
}
cout<<d;
}
1125. Chain the Ropes (25)的更多相关文章
- 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 (25)-贪心水题
贪心水题,每次取最短的两个绳子合并,长度缩减成一半 #include <iostream> #include <cstdio> #include <algorithm&g ...
- 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[一般]
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- 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
题意:略. 思路:思考一下,最先拿去对折的绳子会参与之后的每次对折,而对一条绳子而言,对折的次数越多剩下的就越短,因此,要让最终的结果尽可能长,应该先让较短的绳子先对折. 代码: #include & ...
- PAT1125:Chain the Ropes
1125. Chain the Ropes (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- 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 ...
随机推荐
- 微服务SpringCloud系列
https://my.oschina.net/hmilyylimh?tab=newest&catalogId=5703366
- taihong
揭秘!除了台风 我们还能在卫星云图上看到什么? 2019-08-08 09:20:53 来源: 中国天气网 中国天气网讯 说到卫星云图,可能大多数人首先能想到的就是台风.其实,拥有太空视角的卫星能 ...
- oracle备份和还原
用exp命令即可完成,但要看具体的备份方式. 1. 导出一个完整数据库 exp system/manager file=bible_db log=dible_db full=y 2. 导出数据库定义而 ...
- kafka window安装与配置
一.安装jdk1.8(此处省略) 二.安装zookeeper1. 下载安装包:http://zookeeper.apache.org/releases.html,解压到指定目录(如下) 2. 在系统变 ...
- hook C++
Intercepting Calls to COM Interfaces(hook com接口) 通过COM组件IFileOperation越权复制文件 代码注入之远程线程篇 使用VC++通过远程进程 ...
- ObjectDataSource.ObjectCreating 事件
ObjectCreating 事件在创建由 TypeName 属性标识的对象之前发生. 命名空间:System.Web.UI.WebControls程序集:System.Web(在 system.we ...
- douyu danmu test c#
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Text.Regul ...
- 【web前端】前段时间的面题整理(1)
这是我的试题答案整理,可能有多种答案.我也就写了一两种.在慢慢整合中 第一题 用js实现随机选取10-100之间的10个数字,存入一个数组,去重后求和(保证这10个数字不能出现重复) 要求:去重不能使 ...
- css之——div模拟textarea文本域的实现
1.问题的出现: <textarea>标签为表单元素,但一般用于多行文本的输入,但是有一个明显的缺点就是不能实现高度自适应,内容过多就回出现滚动条. 为了实现高度自适应:用div标签来代模 ...
- .net通用签名方法 webapi签名方法
验证签名方法 [HttpGet] public HttpResponseMessage LockRegister(string 参数1, int 参数2, string 参数3, string 参数4 ...