POJ 3253 Fence Repair

解题思路:基于最小堆实现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];n--;
heapAdjust(
1);
t
+=plank[1];plank[1]=t;
heapAdjust(
1);
ans
+=t;
}
printf(
"%I64d\n",ans);
return 0;
}

 

posted on 2010-12-13 20:44  ltang  阅读(158)  评论(0编辑  收藏  举报

导航