P1090 合并果子——————multiset

版权声明:转载请注明出处 https://blog.csdn.net/Hpuer_Random/article/details/85621213

P1090 合并果子

题目描述

在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。

每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过 n-1n−1 次合并之后, 就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。

因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为 11 ,并且已知果子的种类 数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。

例如有 33 种果子,数目依次为 1 , 2 , 9 。可以先将 1 、 2 堆合并,新堆数目为 3 ,耗费体力为 3 。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为 12 ,耗费体力为 12 。所以多多总共耗费体力 =3+12=15 。可以证明 15 为最小的体力耗费值。

输入输出格式

输入格式:

共两行。
第一行是一个整数 n ( 1 n 10000 ) n(1\leq n\leq 10000) ,表示果子的种类数。

第二行包含 n 个整数,用空格分隔,第 i 个整数 a i ( 1 a i 20000 ) a_i(1\leq a_i\leq 20000) 是第 i 种果子的数目。

输出格式:

一个整数,也就是最小的体力耗费值。输入数据保证这个值小于 2 31 2^{31}

输入输出样例

输入样例

3
1 2 9

输出样例

15

说明

对于30%的数据,保证有n \le 1000n≤1000:

对于50%的数据,保证有n \le 5000n≤5000;

对于全部的数据,保证有n \le 10000n≤10000。


这个题也可以用multiset来写出来

#include<bits/stdc++.h>
// #define SUBMIT
using namespace std;
typedef long long ll;
const int MAXN = 1e4+7;
int main()
{

    #ifdef SUBMIT
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
        ll _begin_time = clock();
    #endif

    int n;
    cin>>n;
    multiset<int>s;
    for(int i=0;i<n;++i)
    {
        int x;
        cin>>x;
        s.insert(x);
    }
    int ans = 0;
    while(s.size()!=1)
    {
        int a = *s.begin(); s.erase(s.begin());//删除元素C. Powers Of TwoC. Powers Of Two
        int b = *s.begin(); s.erase(s.begin());
        ans += a+b;
        s.insert(a+b);
    }
    cout<<ans<<endl;
    // for(multiset<int>::iterator it = s.begin();it!=s.end();it++)
    //     cout<<*it<<" ";
    // cout<<endl;

    #ifdef SUBMIT
        ll _end_time = clock();
        printf("Time: %lld ms\n",_end_time-_begin_time);
    #endif
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hpuer_Random/article/details/85621213