IT民工
加油!

  这道题类似于哈夫曼树,每次切付出的代价等于长度,所以我们要将长的先截出来。

而题目是给出N截,以及每截的长度。所以我们每次加上短的,使总代价最少。学会了优

先级队列的STL写法。

 

/*Accepted    348K    32MS    C++    644B    2012-07-30 16:32:44*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
typedef long long LL;
int N;
LL calc()
{
    LL ans;
    int len, i, a, b, c;
    priority_queue< int, vector<int>, greater<int> > q;
    for(i = 1; i <= N; i ++)
    {
        scanf("%d", &len);
        q.push(len);
    }
    for(i = 0, ans = 0; i < N - 1; i ++)
    {
        a = q.top(), q.pop();
        b = q.top(), q.pop();
        c = a + b;
        ans += c;
        q.push(c);
    }
    return ans;
}

int main()
{
    while(scanf("%d", &N) == 1)
    {
        printf("%lld\n", calc());
    }
    return 0;
}

 

 

 

posted on 2012-04-13 19:15  找回失去的  阅读(128)  评论(0编辑  收藏  举报