POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
//做哈夫曼树时,可以用优先队列(误?)
//这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的)
//哈夫曼树
//64位
//超时->优先队列,,,,
//这道题的优先队列用于取前2个小的元素 #include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; __int64 ans,a[];
int n; struct cmp
{
bool operator ()(__int64 &a,__int64 &b){
return a>b;
}
}; priority_queue<__int64,vector<__int64>,cmp> q; void hfm()
{ __int64 min1,min2,neww,yi;
while(n>)
{
yi=;
min1=q.top();
q.pop();
min2=q.top();
q.pop();
neww=min1+min2;
ans+=neww;
q.push(neww);
n--;
}
} int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
while(!q.empty())q.pop();
for(i=;i<n;i++)
{
scanf("%I64d",&a[i]);
q.push(a[i]);
}
ans=;
if(n>)
hfm();
else
ans=a[];
printf("%I64d\n",ans);
}
return ;
}
//这是我旁边的小学妹写的,不用优先队列也不会超时,可以过耶,我脑子太不灵光了,,,居然一直没想到,,,
//小学妹的AC代码留念:
#include<iostream>
#include<algorithm>
#include<cstdio> using namespace std; int main()
{
int n,length[];
int i,j,sum;
__int64 ans;
while(cin>>n)
{
for(i=;i<n;i++)
cin>>length[i];
sort(length,length+n);
sum = ;
ans = ;
for(i=;i<n-;i++)
{
sum = length[i] + length[i+];
ans += sum;
for(j=i+;j<n;j++)
{
if(sum > length[j])
length[j-] = length[j];
else
{
length[j-] = sum;
break;
}
}
if(j==n)
length[j-] = sum;
}
cout<<ans<<endl;
}
return ;
}
POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)的更多相关文章
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- poj 3253 Fence Repair (水哈夫曼树)
题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
随机推荐
- JavaWeb之 Servlet执行过程 与 生命周期
Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ...
- 集成友盟分享SDK报错
删除4.2.1版本的reference换成4.3版本运行报错 解决办法:要将4.2.1版本的全部库文件物理删除,不要只删除reference.
- scjp考试准备 - 5 - 重载和重写
如下代码,在所指示的位置插入代码能够正常编译: class Alpha{ public void bar(int... x){}; public void bar(int x){}; } public ...
- [转]p2p端口映射工具 dog-tunnel
[转]p2p端口映射工具 dog-tunnel http://www.oschina.net/p/dog-tunnel 狗洞是一个高速的 P2P 端口映射工具,同时支持Socks5代理. 0.5版后开 ...
- 快速生成R语言报告(markdown+Rstudio)
先预览一下用Markdown写的报告[http://rpubs.com/loness/167347],这是HTML格式,你也可以导出Word.pdf,但是导出pdf时文中不能有中文,但是可以使用“pd ...
- 着色Test
1 2 3 4 5 6 7 8 9 10 def test: print "just a test" print "just a test" ...
- LNMP系列网站零基础开发记录(三)
[目录] 扯淡吹逼之开发前奏 Django 开发环境搭建及配置 web 页面开发 Django app开发 Django 站点管理 Python 简易爬虫开发 Nginx&uWSGI 服务器配 ...
- MVC缓存技术
一.MVC缓存简介 缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期.在系统优化过程中, ...
- 22、DDMS(转载)
本文是转载,出处为http://www.xuebuyuan.com/1291595.html 如需删除本文,请私信我,谢谢 DDMS DDMS是一款Google* 提供的应用,可作为独立的工具运行,也 ...
- makefile常用函数
标签(空格分隔): makefile 1.字符串替换和分析函数 $(subst from,to,text) #在文本"text"中使用"to"替换每一处&quo ...