POJ 3253 Fence Repair

贪心水题,用优先队列水过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
long long ans,tll;
priority_queue<long long, vector<long long>, greater<long long> > q;
int main()
{
    int n,i,j,t;
    while (!q.empty())
        q.pop();
    scanf("%d",&n);
    for (i=0; i<n; i++)
    {
        scanf("%d",&t);
        q.push(t);
    }
    ans=0;
    while (q.size() >= 2)
    {
        tll=q.top();
        q.pop();
        tll+=q.top();
        ans+=tll;
        q.pop();
        q.push(tll);
    }
    printf("%lld\n",ans);
    q.pop();
    return 0;
}


 

你可能感兴趣的:(POJ 3253 Fence Repair)