POJ 3253 Fence Repair

来源:互联网 发布:业界良心知乎 编辑:程序博客网 时间:2024/05/17 03:29

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

#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;}


 

原创粉丝点击