poj 3253 fence repair

题目大意:

有一块木板,要把这块木板分成n块,已知每块的长度,每次切木板的花费就是这块模板的长度

现在求把木板切成规定要求的最小花费是多少

思路:

贪心+优先队列

先把n个小木板的长度都加到优先队列里,然后每次取最小的两个,把他们两个加起来加到优先队列里,然后把他们两个的和加到ans里

最后只剩一个木板的时候输出ans

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<queue>
#define Maxn
#define ll long long
using namespace std;
ll n,ans,a,m,g;
int main()
{
    priority_queue <int> q;
    scanf("%lld",&n);
    for(ll i=0;i<n;i++) {scanf("%lld",&a);q.push(0-a);}
    while(q.size()>1)
    {
        m=q.top();
        q.pop();
        g=q.top();
        q.pop();
        ans=ans-m-g;
        q.push(m+g);
    }
    printf("%lld",ans);
}
View Code
原文地址:https://www.cnblogs.com/yyc-jack-0920/p/7216992.html