Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 61005 | Accepted: 20119 |
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=2e4+;
const int maxk=1e4+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
int a[maxn];
void solve()
{
ll ans=;
while(n>)
{
int mi1=,mi2=;
if(a[mi1]>a[mi2]) swap(mi1,mi2);
for(int i=;i<n;i++)
{
if(a[i]<a[mi1])
{
mi2=mi1;
mi1=i;
}
else if(a[i]<a[mi2])
{
mi2=i;
}
}
int l=a[mi1]+a[mi2];
ans+=l;
if(a[mi1]==n-) swap(mi1,mi2);
a[mi1]=l;
a[mi2]=a[n-];
n--;
}
cout<<ans<<endl;
}
int main()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
solve();
return ;
}
上面的算法复杂度是n*n,其实还有一种更快的方法,原理跟上面的一样,但是下面的算法用到优先队列,所以把复杂度降到nlogn
看代码
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=2e4+;
const int maxk=1e4+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
int a[maxn];
priority_queue<int,vector<int>,greater<int> >que;//声明一个从小到大取出数值的优先队列
void solve()
{
ll ans=;
for(int i=;i<n;i++)
que.push(a[i]);//全部存入队列
while(n>)
{
int l1,l2;
l1=que.top();
que.pop();
l2=que.top();
que.pop();
int t=l1+l2;
ans+=t;
que.push(t);
n--;
}
cout<<ans<<endl;
}
int main()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
}
// sort(a,a+n);
solve();
return ;
}
Fence Repair (二叉树求解)(优先队列,先取出小的)的更多相关文章
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- USACO 2006 November Gold Fence Repair /// 贪心(有意思)(优先队列) oj23940
题目大意: 输入N ( 1 ≤ N ≤ 20,000 ) :将一块木板分为n块 每次切割木板的开销为这块木板的长度,即将长度为21的木板分为13和8,则开销为21 接下来n行描述每块木板要求的长度Li ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
随机推荐
- Java父类构造器的讲解
众所周知,对于Java中的所有类而言,它们有一个根父类,即java.lang.Object类. 对于Java中构造器执行的顺序而言,程序执行的顺序为,先执行父类的非静态代码块,然后执行父类的相应的构造 ...
- 03.generator
generatorConfig.xml自动生成连接数据库的这些个公共类的方法. <?xml version="1.0" encoding="UTF-8" ...
- [poj1737]Connected Graph(连通图计数)
题意:输出题中带有$n$个标号的图中连通图的个数. 解题关键: 令$f(n)$为连通图的个数,$g(n)$为非联通图的个数,$h(n)$为总的个数. 则$f(n) + g(n) = h(n)$ 考虑标 ...
- UE mac版16.10.0.22破解
http://bbs.feng.com/read-htm-tid-10828753.html 去官网下载原载,先运行一次,再在终端里执行下面代码就可以破解完成! printf '\x31\xC0\xF ...
- UCSC数据库数据调用cruzdb
https://github.com/Wy2160640/cruzdb UCSC基因组数据库是注释,调节和变异以及越来越多的分类群的各种数据的重要资源. 该库旨在简化数据的利用,以便我们可以进行复杂的 ...
- 1.3 xss原理分析与剖析(4)
0×01 URL编码 URL只允许用US-ASCII字符集中可打印的字符(0×20—0x7x),其中某些字符在HTTP协议里有特殊的意义,所以有些也不能使用.这里有个需要注意的,+加号代表URL编码的 ...
- Cactus项目(又叫MVCAdmin),开源(2016-11-26更新)
Cactus基于之前简单后台管理的改良版本,完善了权限管理,为后续的扩展和管理做了铺垫. 完全开放代码,可供学习交流 目前采用MVC4+Autofac+Dapper制作而成,集成一个简单的Blog和权 ...
- Asp.Net 遍历 循环 显示所有COOKIS,SESSION,Applocation
在C#中循环显示SESSIOn: Response.Write("<br>Session的所有值:<br>"); foreach (string obj i ...
- 交叉编译Spice-gtk
Fedora环境 编译环境 操作系统: 64位 Fedora23 下载源文件 spice-gtk.spice-protocol 安装依赖 $ sudo yum install -y dh-autore ...
- 三种Hash算法对比以及秒传原理.
三种Hash算法对比以及秒传原理 CRC (32/64) MD5 Sha1 分5个点来说 1.校验值长度 2.校验值类别 3.安全级别 4.应用场景 1).校验值长度 CRC(32/64) 分别 ...