1125 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). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 1.
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段绳子,要求将着N段绳子串联起来,寻找传来的策略使最后串联出来的绳子最长。两个绳子串联就是将两个绳子对折之后使其连接在一起。
思路:
最先串联的绳子对折的次数最多,所以应该将短的绳子先串联起来,这样可以保证长绳子的对折次数最少,从而保证最后的长度最长。
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() { 6 int n; 7 cin >> n; 8 vector<int> v(n); 9 for (int i = 0; i < n; ++i) cin >> v[i]; 10 sort(v.begin(), v.end()); 11 double ans = v[0]; 12 for (int i = 1; i < n; ++i) { 13 ans = (ans + v[i]) / 2; 14 } 15 cout << (int)ans << endl; 16 return 0; 17 }
刚开始做的时候没有理解题意,以为可以不用将所有的绳子都串联进来就可以得到最大长度(将两个15单位长的绳子对折之后绳子的长度还是15,这样不就比14还要长了吗?),但是题目中有这样一句话“you are supposed to chain them into one rope.”,通过这句话我们可以知道题目是要我们将所有的绳子都串联起来。参考了(https://www.liuchuo.net/archives/3741)
相关文章
- ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
- fatal: The upstream branch of your current branch does not match the name of your current branch
- How to get the parents of a merge commit in git? 父结点
- The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
- The platform of the target `Pods` (iOS 4.3) is not compatible 错误
- UVA 1025 A Spy in the Metro
- Getting and installing the PEAR package manager
- Microsoft SQL Server 17导出xlsx文件时报错:The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. (System.Data)
- QMutex“A mutex must be unlocked in the same thread that locked it”解决(在run里创建对象是不二法宝)
- AndroidStudio:The application‘s minSdkVersion is newer than the device API level.
- PHP错误:SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
- Mysql_解决The total number of locks exceeds the lock table size错误
- Unity 报错之 The same field name is serialized multiple times in the class or its parent class.
- [LeetCode] 1233. Remove Sub-Folders from the Filesystem 删除子文件夹
- The declared package does not match the expected package
- The model backing the context has changed since the database was created.
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
- 圣诞特辑-解决idea新建报错The required Java version 11 is not supported by the project SDK 1.8.