///优先队列,每次取两个最小的木板的长度,再把这个长度放进队列,反复取
# 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;
}
poj 3253 Fence Repair (优先队列)
最新推荐文章于 2023-07-26 07:45:10 发布