PAT甲级 1125. Chain the Ropes (25)
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
————————————————————————————————
题目的意思是将n个绳子两两合并,每次长度变为和的一半,问最后变成1根的长度最大是多少
思路,每次合并每一根绳子会变为原来的1/2,所以最长的肯定合的越少越好,贪心选择最短的合并
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; int main()
{
int n;
int a[100005];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=1;i<n;i++)
{
a[i]=(a[i]+a[i-1])/2;
}
printf("%d\n",a[n-1]);
return 0;
}
PAT甲级 1125. Chain the Ropes (25)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- 【PAT甲级】1083 List Grades (25 分)
题意: 输入一个正整数N(<=101),接着输入N个学生的姓名,id和成绩.接着输入两个正整数X,Y(0<=X,Y<=100),逆序输出成绩在x,y之间的学生的姓名和id. tric ...
随机推荐
- FastDFS集群安装
集群安装1.FastDFS安装 Tracker,nginx 192.168.56.113 192.168.56.114 storage group1 192.168.56.115 ...
- 三种简单排序算法(java实现)
一.冒泡排序 算法思想:遍历待排序的数组,每次遍历比较相邻的两个元素,如果他们的排列顺序错误就交换他们的位置,经过一趟排序后,最大的元素会浮置数组的末端.重复操 作 ...
- centos 7下安装mysql
可参考: http://blog.csdn.net/xyang81/article/details/51759200 1.去mysql官方网站查询最新的版本: 2.运行指令: wget http:// ...
- tomcat+servlet例子
在C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\app\WEB-INF文件夹下建立文件夹classes. 在C:\Pro ...
- Zxing2.1扫描取景框变形问题解决
修改竖屏扫描的贴子,2.0之前的都很适用.可是到了2.1,有些贴子的做法可以将扫描框改为竖屏,但是取景框里扫描到的东西是变形的(扁的),本人仔细研究一番,终于解决了这个问题,下面贴出解决办法: 1.修 ...
- windows 2003端口80system进程占用的情况
1.首先是http服务 a. 位置 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTPb. 把 REG_DWORD 类型的项 Start ...
- IT资产管理详解
- 2018.06.27"Shortest" pair of paths(费用流)
"Shortest" pair of paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1589 A ...
- openstack之flavor管理
概览 [root@cc07 ~]# nova help | grep flavor flavor-access-add Add flavor access for the given tenant. ...
- CentOS 7 / RHEL 7 运行单用户模式进行root的密码重置
步骤一,开机时随便按下键盘,进入以下菜单 步骤二: 选择第一项,按e键进行修改 步骤三,定位到 ro( linux 16 or linuxefi ) 步骤四:把ro改成 “rw init=/sys ...