PAT甲级 1125

news/2024/10/21 3:48:49/

PAT甲级 1125

    • 题目 Chain the Ropes
    • 解析
    • 代码

题目 Chain the Ropes

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≤10​^4​​ ). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 10^​4​​ .
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.

解析

贪心算法,将所给的数据从小到大排序,按序除二相加。

代码

#include<bits/stdc++.h>
#include<string>#define INF 1<<29using namespace std;void pat1125() {int n;double sum = 0;int data[10005];cin >> n;for (int i = 0; i < n; ++i) {cin >> data[i];}sort(data, data + n);sum = data[0];for (int i = 1; i < n; ++i) {sum += data[i];sum/=2.0;}printf("%d\n",(int)sum);
}int main() {pat1125();return 0;
}

http://www.ppmy.cn/news/373627.html

相关文章

P1125

数组/桶的思想&#xff1a;笨小猴 要求 1.计算一个单词中每个字母出现的次数&#xff0c;并找出出现的次数与出现最小的次数 方案 1.利用桶的思想 桶算法模板 用处&#xff1a;记录数组或字符串中每个值出现的次数 场景&#xff1a;字符串&#xff0c;数组&#xff0c;字…

1125 Chain the Ropes

题目 题意&#xff1a;将两段绳子对折连接在一起可获得的最大长度 #include<iostream> #include<algorithm> using namespace std; int main() {int n;cin>>n;int s[n];for(int i0; i<n; i)cin>>s[i];sort(s,sn);int anss[0];for(int i1; i<n;…

洛谷P1125 笨小猴 题解

洛谷 P1125 [NOIP2008 提高组] 笨小猴 题解 题目描述 笨小猴的词汇量很小&#xff0c;所以每次做英语选择题的时候都很头疼。但是他找到了一种方法&#xff0c;经试验证明&#xff0c;用这种方法去选择选项的时候选对的几率非常大&#xff01; 这种方法的具体描述如下&#xf…

信息学奥赛一本通:1125:矩阵乘法

1125&#xff1a;矩阵乘法 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 34833 通过数: 18995 【题目描述】 计算两个矩阵的乘法。nm阶的矩阵A乘以mk阶的矩阵B得到的矩阵C 是nk阶的&#xff0c;且C[i][j] A[i][0]B[0][j] A[i][1]B[1][j] …… A[i][m-1]B[m-…

HLK-LD1125H-24G人体测距微波雷达

** HLK-LD1125H-24G人体测距微波雷达 ** 概述 转载官方概述&#xff1a; HLK-LD1125H-24G 是一款高灵敏度 24GHz 毫米波人体存在检测雷达模块。区别于传统雷达通过检测人体移动的大幅度动作或者微小幅度的肢体动作来判断人体存在&#xff0c;本模块主要特点是通过检测积累人…

poj 1125

题目简答&#xff0c;考floyd算法。代码如下&#xff1a; #include<iostream> #include<iomanip> #include<cstdlib> #include<string.h> #define INTMAX 10000 using namespace std;int visit[101]; int path[101][101]; int cons[101][101]; int n;v…

京瓷1125打印机清零_怎么设置京瓷1125MFP打印机ip地址

在 确认打印机 电脑都是没问题的情况下 1. 首先,将光盘放入光驱中。如果光盘没有自动运行,请直接跳到 第2步 ;如果光盘自动运行,将会出现如下窗口,如图 4 开始安装所示。点击“取消”按钮,退出 " 系列安装程序” 的安装向导。 2. 插上打印机的电源线,打开打印机电源并装好…