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 ...
随机推荐
- 表单 - Form - EasyUI提供的表单异步提交
方案一 被提交的表单 <form id="loginForm" method="post"> <table align="cente ...
- BZOJ 1251: 序列终结者
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3773 Solved: 1579 [Submit][Status][Dis ...
- java多线程系类:基础篇:02常用的实现多线程的两种方式
本章,我们学习"常用的实现多线程的2种方式":Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多 ...
- DEDECMS之六 网站地图、RSS地图
在用织梦CMS做网站的都知道,在它的robots.txt是屏蔽掉了data目录的,可是,不巧dedecms默认的网站地图是在data下的,为了让蜘蛛更好的爬行,有必要将dedecms生成的网站地图放在 ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- Kafka及 .NET Core 客户端
消息队列 Kafka 的基本知识及 .NET Core 客户端 消息队列 Kafka 的基本知识及 .NET Core 客户端 前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Linux 信号详解五(信号阻塞,信号未决)
信号在内核中的表示 执行信号的处理动作成为信号递达(Delivery),信号从产生到递达之间的状态称为信号未决(Pending).进程可以选择阻塞(Block)某个信号. 被阻塞的信号产生时将保持在未 ...
- Python之线程、进程和协程
python之线程.进程和协程 目录: 引言 一.线程 1.1 普通的多线程 1.2 自定义线程类 1.3 线程锁 1.3.1 未使用锁 1.3.2 普通锁Lock和RLock 1.3.3 信号量(S ...
- CoffeeScript的类继承的工具函数extends
__hasProp = {}.hasOwnProperty, __extends = function(child, parent) { // 派生类时,如果基类的类属性值是对象,那么子类的类属性只是 ...