poj Fence Repair 贪心

来源:互联网 发布:知乎大学排名 编辑:程序博客网 时间:2024/05/17 06:51

反过来做,就是要合成一个板,容易想到每次选取最小的两块合并,结果最优。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define ll long longusing namespace std;const int maxn=2e4+9;int main(){    int n;    priority_queue <ll,vector<ll>,greater<ll> > q;    while(scanf("%d",&n)!=EOF)    {        while(!q.empty()) q.pop();        for(int i=1,tmp;i<=n;i++)        {            scanf("%d",&tmp);            q.push(tmp);        }        long long ans=0;        for(int i=1;i<n;i++)        {            long long tmp=0;            tmp+=q.top(),q.pop();            tmp+=q.top(),q.pop();            ans+=tmp;            q.push(tmp);        }        cout<<ans<<endl;    }    return 0;}


原创粉丝点击