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 Npositive 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
分析:贪心题,自己做的时候直接排序然后从小开始两两对折最后输出结果就AC了,但不知道原因。。(做贪心有时候就凭感觉,没有严格证明)。后来看柳神题解才恍然大悟,若从大的开始对折,每次对折损耗会更多,而从小的开始损耗的就越少。比如10000,100,10,1,我从10000开始对折的话总共对折3次,相当于会损耗大的部分3次,如果最后再对折就损耗少了!
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-28-13.41.03 * Description : A1125 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; int a[maxn]; int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int n; scanf("%d",&n); ;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); ]; ;i<n;i++){ sum=(a[i]+sum)/; } cout<<int(sum); ; }
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 ...
- 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甲题题解-1125. Chain the Ropes (25)-贪心水题
贪心水题,每次取最短的两个绳子合并,长度缩减成一半 #include <iostream> #include <cstdio> #include <algorithm&g ...
- 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 甲级 1079 Total Sales of Supply Chain (25 分)(简单,不建树,bfs即可)
1079 Total Sales of Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributor ...
随机推荐
- GinKgoCTF-Crypto
一:古典密码 一只小羊跳过了栅栏,密码如下: GpKlCeT_FC{rAy_pStiym} 栅栏解密——>11栏 二:滴滴滴 --./-.-/-.-./-/..-./----.--/-../.. ...
- Spring Boot 揭秘
SpringBoot基础 微服务 解决大一统的服务化架构的问题 代码冲突问题 交付复杂,影响面大 测试困难 微服务的好处 可扩展性 隔离性 灵活性,多语言多技术生态 微服务的挑战 保持微服务的互通性 ...
- Linux 命令:文件目录操作与实例
来源: http://blog.51cto.com/yuanzhitang/2056994 本文介绍基础的文件操作:创建,移动,编辑,删除 文件和文件夹 命令与案例: mkdir 创建目录 - ...
- streamdataio 实时数据分发平台
streamdataio 是一个实时的数据分发平台(当然是收费的,但是设计部分可以借鉴),我们可以通过这个平台 方便的拉取rest api 数据,或者发布数据到后端,streamdataio 可以帮助 ...
- Day 35数据库(Day1)
创建表. create table student( id int not null auto_increment PRIMARY key, name archar(250) not null, ag ...
- 【转】C#获取当前日期时间(转)
我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...
- git与github建立仓库连接步骤
一.先对git 进行用户设置 首先你得在网上下载git软件并且安装,一路默认安装就好了,然后就可以开始本地仓库的建立了.打开你安装好的git, 在开始菜单里面找到git文件夹里面的git bash端 ...
- [转]MyBatis中resultType与resultMap区别
MyBatis中关于resultType和resultMap的具体区别如下: MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap.resu ...
- sql server 附加只有mdf的数据库文件
有时候SQL Server意外断电会导致SQL Server的ldf日志文件丢失或者损坏,这个时候你如果直接附加mdf文件到SQL Server会失败,这里提供一个方法可以还原只有mdf的数据库文件, ...
- git 项目配置用户名、邮箱的方法
git 项目配置用户名.邮箱的方法 单个仓库里,配置用户名.邮箱: git config user.name "姓名" git config user.email "邮箱 ...