您的位置:首页 > Web前端

POJ 3253 Fence Repair

2010-12-13 20:44 260 查看
解题思路:基于最小堆实现Haffman

NULL

#include <iostream>
using namespace std;
int n,plank[20001];
void heapAdjust(int s)
{
int i,exp;
for(i=s;i<=(n/2);)
{
exp=((i*2+1)<=n&&plank[i*2+1]<plank[i*2])?1:0;
if(plank[i]>plank[i*2+exp])
swap(plank[i],plank[i*2+exp]),i=i*2+exp;
else break;
}
}

int main()
{
int i,t;
__int64 ans=0;
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&plank[i]);
for(i=n/2;i>=1;i--)heapAdjust(i);
while (n>1)
{
t=plank[1];plank[1]=plank
;n--;
heapAdjust(1);
t+=plank[1];plank[1]=t;
heapAdjust(1);
ans+=t;
}
printf("%I64d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: