0
点赞
收藏
分享

微信扫一扫

poj3235 Fence Repair【优先队列】


题目链接:​​http://poj.org/problem?id=3253​​​
题意:有一块木板,让你把他分成L块,告诉你每块的长度,问你如何分,能使得花费的代价最小,每次的代价为,分成的两块木板的长度之和
解析:想了一下,分的过程其实也就是合的逆过程,那么你就考虑合成木板的情况,每次合最小的两块是最优的,每次合完还要放回去,直至合成最终心态,才算完成操作,我采用优先队列的姿势过的题

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 12005;
int main(void)
{
int n;
scanf("%d",&n);
priority_queue<long long,vector<long long>,greater<long long> >q;
for(int i=0;i<n;i++)
{
long long x;
scanf("%I64d",&x);
q.push(x);
}
long long ans = 0;
while(!q.empty())
{
long long t1 = q.top();
q.pop();
long long t2 = q.top();
if(q.empty())
break;
q.pop();
q.push(t1+t2);
ans += t1+t2;
}
printf("%I64d\n",ans);

return 0;
}


举报

相关推荐

0 条评论