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 ...
随机推荐
- DTAP street
一个网站程序的上线一般要经过开发[Development]测试[Testing]验收[Acceptance]生产[Production].所以又叫做DTAP street.对应有开发环境.测试环境.验 ...
- PHP 学习笔记 01
例子: 为什么要学PHP 主观原因: 前段时间在学校处理了毕业的一些事情,回到上海后开始了找工作的旅程.意向工作是WPF开发或者ASP.NET 作为后端的WEB开发. 陆陆续续一直在面试,其中有一家公 ...
- .NET开源工作流RoadFlow-流程设计-流转条件设置(路由)
当一个步骤后面有多个步骤时,可以设置为根据设置条件系统自动判断该流向哪些步骤,也叫路由. roadflow没有单独的路由步骤来设置条件,流程条件通过双击连线弹出条件设置框来设置. 1.sql条件 即通 ...
- hdu 5264 pog loves szh I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...
- Android内存管理机制
相信一步步走过来的Android从业者,每个人都会遇到OOM的情况.如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力. 今天我们就谈谈在Android平台下内存的管理之道,开始 ...
- 记录bigdesk中ElasticSearch的性能参数
定时采集bigdesk中的Elasticsearch性能参数,并保存到数据库或ELK,以便于进行长期监控. 基于python脚本实现,脚本如下: #coding=gbk import httplibi ...
- Eclipse Indigo 3.7.0 安装GIT插件提示 requires 'bundle org.eclipse.team.core(转)
文章参考来源:http://showlike.iteye.com/blog/1958538 错误提示: Cannot complete the install because one or more ...
- mysql 字段编码该为utf8mb4
alter table c_comment modify column content varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unico ...
- Notes of the scrum meeting(12.7)
meeting time:18:30~19:10p.m.,December 7th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- CoffeeRobotTeam项目组报告
一.小组分工 模块 任务 责任人 备注 报告 需求分析 熊振威 功能分析 熊振威 项目报告 熊振威 人机界面 秦勤.洪超 单元测试 姜进.张文强 机器人代码 机器人类 徐意.余拥军.孙智博 机器人运动 ...