您的位置:首页 > 其它

POJ3264 Balanced Lineup

2015-08-19 11:11 183 查看
题目大意:Framer John有一段木板,想用以建筑围墙,没有锯子的他只好向Framer Don求助。FD提出要求,FJ每截开一段木板,就要给这段木板长度的钱。FJ想使花费最少,向你求助。

思路:绝对是合并果子的翻版!把截木板当成合并木板就行了。小心L、n的范围,最终的ans用int装是装不下的,要用long long。

代码如下:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n;
priority_queue<long long> q;

void init()
{
    scanf("%d",&n);
    int tmp;
    for (int i=1;i<=n;++i)
    {
        scanf("%d",&tmp);
        q.push(-tmp);
    }
}

void work()
{
    int ans=0;
    while (!q.empty())
    {
        int a=q.top();
        q.pop();
        if (q.empty())
        {
            printf("%lld",-ans);
            return;
        }
        int b=q.top();
        q.pop();
        ans+=(a+b);
        q.push(a+b);
    }
}

int main()
{
    init();
    work();
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: