【BZOJ】1724 [Usaco2006 Nov]Fence Repair 切割木板

【算法】贪心+堆

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=20010;
int n,heap[maxn],sz;
void heap_push(int x)
{
    heap[++sz]=x;//新数入堆底 
    int now=sz;//以堆底为起点 
    while(now>1&&heap[now]<heap[now>>1])//非根节点的父亲>儿子时------注意非根判断 
     {
         swap(heap[now],heap[now>>1]);//交换即上推 
         now>>=1;//转移到父亲 
     }
}
int heap_pop()
{
    int ans=heap[1];//取出答案 
    heap[1]=heap[sz--];//将堆底最后一个元素调上来 
    int now=1;//以堆顶为起点 
    while(now<=(sz>>1))//若now有儿子------儿子存在判断 
     {
         int next=now<<1;//令next为now的左儿子------儿子赋变量 
         if(next<sz&&heap[next]>heap[next|1])next++;//now有右儿子且右儿子更小时,令next为右儿子------左右儿子判断---注意右儿子存在判断 
         if(heap[next]>heap[now])return ans;//若根比儿子小,满足条件,退出 
          else
           {
               swap(heap[now],heap[next]);//交换即下推 
               now=next;//转移到儿子 
           }
     }
    return ans; 
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
     {
         int u;
         scanf("%d",&u);
         heap_push(u);
     }
    long long ans=0;
    for(int i=1;i<n;i++)
     {
         int u=heap_pop(),v=heap_pop();
         heap_push(u+v);
         ans+=u+v;
     }
    printf("%lld",ans);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/onioncyc/p/6212840.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值