poj 3253 Fence Repair (优先队列)

来源:互联网 发布:数据库包括哪些语句 编辑:程序博客网 时间:2024/05/21 20:23
///优先队列,每次取两个最小的木板的长度,再把这个长度放进队列,反复取# include <stdio.h># include <algorithm># include <iostream># include <string.h># include <queue># include <vector>using namespace std;///长度小的放在前面class cmp{public:    bool operator ()(const __int64 a,const __int64 b)const    {        return a>b;    }};int main(){    int n;    __int64 temp,sum,p1,p2;    while(~scanf("%d",&n))    {        priority_queue<__int64,vector<__int64>,cmp>q;///定义优先队列        while(n--)        {            scanf("%I64d",&temp);            q.push(temp);        }        sum=0;        while(q.size()>1)        {            p1=q.top();            q.pop();            p2=q.top();            q.pop();            temp=p1+p2;            sum+=temp;            q.push(temp);        }        printf("%I64d\n",sum);    }    return 0;}

0 0