bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,a;
typedef long long ll;
ll ans;
void read(int &x){
x=; int f=; char ch;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') f=-;
for (;isdigit(ch);ch=getchar()) x=x*+ch-''; x*=f;
}
priority_queue< int,vector<int>,greater<int> >heap;
int main(){
read(n),ans=;
for (int i=;i<=n;i++) read(a),heap.push(a);
for (int x,y,i=;i<n;i++){
x=heap.top(),heap.pop();
y=heap.top(),heap.pop();
ans+=x+y; heap.push(x+y);
}
printf("%lld\n",ans);
return ;
}
做法:堆入门题,学习系统堆的用法——>雾(:
大根堆:priority_queue<int>heap;
小根堆:priority_queue< int,vector<int>,greater<int> >heap;
heap.top() 函数:堆顶的元素值;
heap.pop()过程:弹出堆顶元素;
heap.empty()函数:堆空与否,堆空则返回1,否则返回0;
heap.push()过程:在堆中加入一个元素,并维护堆的性质。
bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板的更多相关文章
- bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)
一开始被题目读错题= =以为每次只能割一块,那么就是从大到小切 但是其实是可以分为几堆来切的 所以可以逆着来,变为合并n个木板代价最小 易证每次找最小的两堆合并代价最小 用优先队列维护堆..偷偷懒= ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板
题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )
倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板
1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 854 Solved: 42 ...
- BZOJ 1724 [Usaco2006 Nov]Fence Repair 切割木板:贪心 优先队列【合并果子】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 题意: 你要将一块长木板切成n段,长度分别为a[i](长木板的长度 = ∑ a[i] ...
- [Usaco2006 Nov] Fence Repair 切割木板
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1356 Solved: 714[Submit][Status][Discuss] Description ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考
Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...
- bzoj 1724: [Usaco2006 Nov]Fence Repair 切割木板【堆】
如果反着看,看成合并木板,就和合并果子一样了,把若干块放进一个小根堆,然后每次取出两个合并,把合并结果加进答案和堆里 代码里小根堆用优先队列实现(懒 #include<iostream> ...
- 【BZOJ】1724 [Usaco2006 Nov]Fence Repair 切割木板
[算法]贪心+堆 #include<cstdio> #include<algorithm> using namespace std; ; int n,heap[maxn],sz ...
随机推荐
- 方差分析 ANOVA
来源: http://blog.sciencenet.cn/blog-479412-391481.html 方差分析是为了比较多个总体样本均数是否存在差别.该方法有RA.Fisher首先提出,后来由G ...
- swift 集合类型(二)
说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...
- ASP.NET 系列:单元测试之StructureMap
ASP.NET使用StructureMap等依赖注入组件时最重要就是EntityFramework的DbContext对象要保证在每次HttpRequest只有一个DbContext实例,这里将使用第 ...
- 关于安装Visual Studio 2015 RC版卡主不动的解决方案
在官方网站下载了vs_community.exe自动下载安装的软件进行安装,安装到github时候 卡了1个小时 一直显示正在获取,遂感觉不大对劲,使用了FQ程序,过了2分钟果然正常获取安装,进入了下 ...
- DbEntry在Vs2012里的配置
dbentry官方的版本还不支持vs2012,要再vs2012中使用,必须做下调整 1:新建类库项目,然后添加dbentry 的dll引用. 2:在建好的类库项目中.csproj 新添加了类库项目后, ...
- 转一篇Unity的相机动画控制
最近真是忙,连研究细看的时间都没有了,原帖地址:https://alastaira.wordpress.com/2013/11/08/smooth-unity-camera-transitions-w ...
- HDU5878~HDU5891 2016网络赛青岛
A.题意:给出一个整数n, 找出一个大于等于n的最小整数m, 使得m的质因数只有2 3 5 7 分析:预处理出质因数2 3 5 7的数,超过maxt就行,然后找 B.题意:求1/1^2+1/2^2+. ...
- [BZOJ3142][HNOI2013]数列(组合)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3142 分析: 考虑差值序列a1,a2,...,ak-1 那么对于一个确定的差值序列,对 ...
- Tomcat本地服务器搭建
首先,下载jdk-8u111-windows-x64.exe,然后配置环境,以安装目录D:\jdk1.8.0_111为例: 新建一个变量: 然后打开path新建两个变量: 最后去控制台敲javac或者 ...
- tornado和django的结合使用 tornado Server for django WSGI APP
#!/usr/bin/env python # Run this with # Serves by default at # http://localhost:8080/hello-tornado a ...