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 ...
随机推荐
- EmguCV学习——简单使用
关于EmguCV我就不多说了,是对应于OpenCV的一套net库. 公司是视觉方面的业务,我又不会c++(好想会啊,正在学习中).由于各种需求,自己觉得对c++不是特别感冒,所以选用了net下的ope ...
- Java学习中,常用的命令管理(Java 学习中的小记录)
Java学习中,常用的命令管理 作者:王可利(Star·星星) 一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始 ---->所有程序---& ...
- .NET开源工作流RoadFlow-表单设计-新建表单(属性设置)
点击表单设计工具栏上的 新建表单 按钮会弹出新表单属性设置框: 表单名称:新表单表名称. 数据连接:表单对应的数据库连接(此连接在 系统管理-->数据库连接 中维护). 数据表:表单对应的数据库 ...
- poj 1383 Labyrinth
题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...
- hdu 1277 全文检索
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1277 全文检索 Description 我们大家经常用google检索信息,但是检索信息的程序是很困难 ...
- OpenStack:安装Keystone
>安装Keystone1. 安装# apt-get install keystone2. 创建dbcreate database keystone;grant all privileges on ...
- 微信扫码支付asp.net(C#)实现步骤
支付提交页面: [HttpPost] public ActionResult index(decimal amount) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯 ...
- WCF note1
Summary of WCF Client to use service use ChannelFactory to create proxy to use service. Client code ...
- DataTemplate和ControlTemplate联系与区别
---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...
- VMWare Workstation 11安装CentOS7,附图 [原创]
1.新建虚拟机 2.新建虚拟机向导,选择典型 3.选择稍后安装操作系统 4.选择linux版本,注意:宿主系统是64位的,此处就得选64位:宿主系统是32位的,此处就得选32位 5.选择路径 6.指定 ...