【算法】贪心+堆

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

【BZOJ】1724 [Usaco2006 Nov]Fence Repair 切割木板的更多相关文章

  1. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板

    题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer ...

  2. BZOJ 1724 [Usaco2006 Nov]Fence Repair 切割木板:贪心 优先队列【合并果子】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 题意: 你要将一块长木板切成n段,长度分别为a[i](长木板的长度 = ∑ a[i] ...

  3. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考

    Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...

  4. bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】

    如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...

  5. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

  6. 1724: [Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 854  Solved: 42 ...

  7. [Usaco2006 Nov] Fence Repair 切割木板

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1356  Solved: 714[Submit][Status][Discuss] Description ...

  8. 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心

    堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...

  9. bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)

    一开始被题目读错题= =以为每次只能割一块,那么就是从大到小切 但是其实是可以分为几堆来切的 所以可以逆着来,变为合并n个木板代价最小 易证每次找最小的两堆合并代价最小 用优先队列维护堆..偷偷懒= ...

随机推荐

  1. EasyJSWebView原理分析

    概述 在iOS6之前,native只能调用webiew里的js代码,官方没有提供js调用native方法的接口.到了iOS7,官方提供了JSContext用来与js交互,native和js可以双向调用 ...

  2. C#命名参数

    文章:史上最全的ASP.NET MVC路由配置,以后RouteConfig再弄不懂神仙都难救你啦~ 命名参数规范+匿名对象 routes.MapRoute(name: "Default&qu ...

  3. hexo设置permalink-避免url中出现中文

    hexo博客初始化的url是年月日+题目:year/:month/:day/:title/,这样的url不便与分享,中文会乱吗,而且一旦修改了题目(我相信大部分人的题目都是中文)就会导致之前分享的ur ...

  4. iOS关于setContentOffset的一些细节问题

    在UIScrollView,setContentOffset方法的功能是跳转到你指定内容的坐标, setContentOffset有两种方法:setContentOffset:和setContentO ...

  5. Firefox火狐浏览器 修改默认搜索引擎

    如图:

  6. KindEditor是一套很方便的html编译器插件

    KindEditor是一套很方便的html编译器插件.在这里做一个简单的使用介绍. 首先在官网上下载最新的KindEditor文件(里面有jsp,asp等不同版本文件夹,可以删掉你不需要的版本), 把 ...

  7. django为model设置表名

    class redis_data(models.Model):     class Meta:         db_table='redis_data'     key=models.CharFie ...

  8. Stax解析XML示例代码

    package org.itat.stax; import java.io.IOException; import java.io.InputStream; import javax.xml.pars ...

  9. vscode Variables Reference

    vscode Variables Reference 您可以在以下链接中找到该列表:https://code.visualstudio.com/docs/editor/variables-refere ...

  10. Springboot2.x+shiro+redis(Lettuce)整合填坑

    主要记录关键和有坑的地方 前提: 1.SpringBoot+shiro已经集成完毕,如果没有集成,先查阅之前的Springboot2.0 集成shiro权限管理 2.redis已经安装完成 3.red ...