zoukankan      html  css  js  c++  java
  • Problem K Length of Bundle Rope

    Problem K Length of Bundle Rope

    题意:给你 n 个箱子的大小 a ,要求把箱子捆成一个单位,每次将两个单位捆在一起消耗长度为两个单位的大小只和的绳子,要求绳子长度最小。

    样例解释:

    8 5 14 26

    题面的样例有毒看了半天。

    应该是 27+26=53

    每次取出大小最小的箱子捆成一个单位就可以保证消耗最少。

    1.8+5=13;

    2.13+14=27;

    3.27+26=53;

    res=13+27+53=93;

    可以使用小顶堆解决这类问题。

    代码:

    #include <set>
    #include <map>
    #include <list>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <ctime>
    #include <iomanip>
    //#include <unordered_map>
    #define INF 0x3f3f3f3f
    #define ll long long
    #define ull unsigned long long
    #define FILL(a,n,v) fill(a,a+n,v)
    #define Mset(a,v) memset(a,v,sizeof a)
    #define Mcpy(a,b) memcpy(a,b,sizeof b) //a=b
    #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
    #define fcin freopen("/Users/lihaji/Documents/program/XcodeTest/Test/in.txt","r",stdin)
    #define fcout freopen("out.txt","w",stdout)
    using namespace std;
    
    priority_queue<int,vector<int>,greater<int>>q;
    int t;
    int n;
    int a;
    int main()
    {
    //    fcin;
        cin>>t;
        while(t--)
        {
            
            cin>>n;
            for(int i=0;i<n;i++)
            {
                cin>>a;
                q.push(a);
            }
            int res=0;
            while(q.size()>1)
            {
                int x=q.top();
                q.pop();
                int y=q.top();
                q.pop();
                res+=x+y;
                q.push(x+y);
            }
            q.pop();
            cout<<res<<endl;
        }
    }
    
    
  • 相关阅读:
    【洛谷P3649】回文串
    【CH2401】送礼物
    Python 函数
    Python 文件操作
    JS的DOM和BOM
    JavaScript (内置对象及方法)
    JavaScript 基础(数据类型、函数、流程控制、对象)
    Python基础数据类型(数字、字符串、布尔、列表、元组、字典、集合等)
    MySQL(二)
    数据备份 及 Python 操作 Mysql
  • 原文地址:https://www.cnblogs.com/lihahahahaji/p/14098912.html
Copyright © 2011-2022 走看看