PAT甲级1125
来源:互联网 发布:java仿qq聊天室源代码 编辑:程序博客网 时间:2024/06/05 04:03
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:810 15 12 3 4 13 1 15Sample Output:
14
#include<cstdio>#include<stack>#include<vector>#include<algorithm>using namespace std;int main(){int N;scanf("%d", &N);vector<int> v(N,0);for (int i = 0; i < N; i++){scanf("%d", &v[i]);}sort(v.rbegin(), v.rend());stack<int> s;for (int i = 0; i < N; i++){s.push(v[i]);}int a, b;while (s.size() > 1){a = s.top(); s.pop();b = s.top(); s.pop();s.push((a + b) / 2);}printf("%d\n", s.top());return 0;}
- PAT甲级1125
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- PAT(甲级)1015
- PAT(甲级)1016
- Hexo+GitHub pages 搭建个人博客
- 文本框仅可接收decimal
- jq获取表单值与赋值代码
- caffe学习笔记18-image1000test200数据集分类与检索完整过程
- select row_number() over (order by ItemID asc) as RowIndex
- PAT甲级1125
- HDU2571 命运(DP)
- 浮华背后与我的路-----关于华为裁员
- 一道非常有趣的多态
- SQL-to_char()保留小数,千位分隔符
- log4j.properties 基本配置
- 跑表精确至0.1秒
- JVM讲解
- 最简单的 RabbitMQ 监控方法