zoukankan      html  css  js  c++  java
  • PAT 1125 Chain the Ropes[一般]

    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.

    rope.jpg

    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 (2N104​​). 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个数里最大的两个数呗,那折叠之后不就是最长的了吗???

    //似乎明白了,因为组合成的绳子可以再次被折叠,所以就有一个最优解的。

    //我似乎理解错题意了,是要使用全部N条绳子才可以!

    #include <iostream>
    #include<vector>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    
    int main(){
        int n,t;
        cin>>n;
        //是哪个头文件里的???
        priority_queue<int> pq;//使用优先队列存储
        for(int i=0;i<n;i++){
            cin>>t;
            pq.push(t);
        }
        int a,b,mlen=0;
        while(!pq.empty()){
            a=pq.top();pq.pop();
            b=pq.top();pq.pop();
            int m=a/2+b/2;
            if(m>mlen){
                mlen=m;
            }else break;
            pq.push(m);
        }
        cout<<mlen;
        return 0;
    }
    View Code

    //理解错误的题意。

    柳神解答:

    1.不过还是复习了优先队列的头文件要包括queue。

  • 相关阅读:
    Spring Security【一】 ------ 前后端分离开发
    mybatis 使用mybatis-plus-generator进行代码自动生成
    VuejsApp简介
    BeetleX之Vue ElementUI生成工具
    BeetleX数据分析中间服务V3
    vuejs集成echarts的一些问题
    BeetleX使用bootstrap5开发SPA应用
    BeetleX大数据之产品分析服务
    小试牛刀ElasticSearch大数据聚合统计
    在windows 10的ubuntu系统上如何使用dd命令写u盘?
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9876257.html
Copyright © 2011-2022 走看看