题目

//做哈夫曼树时,可以用优先队列(误?)

//这道题教我们优先队列的一个用法:取前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(优先队列,哈夫曼树,模拟)的更多相关文章

  1. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  2. poj 3253 Fence Repair (水哈夫曼树)

    题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的 ...

  3. POJ 3253 Fence Repair (哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19660   Accepted: 6236 Des ...

  4. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  5. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  6. poj3253 Fence Repair【哈夫曼树+优先队列】

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  7. POJ - 3253 Fence Repair 优先队列+贪心

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  8. poj 3253 Fence Repair(优先队列+huffman树)

    一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...

  9. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  10. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

随机推荐

  1. ruby on rails 实战(二)

    1,修改routes文件,让所有的action都可以使用get或者post方式访问 match "/:controller/:action" => "control ...

  2. 【PHP】PHP中的类与对象

    面向对象并不是PHP的关键,但PHP确实能很好的支持面向对象编程,而且类与对象也成为了PHP5的核心组成部分.PHP的面向对象特性让构建大型的复制应用成为可能,有了类与对象,就自然产生了各种编程范式和 ...

  3. 【WPF学习日记】——Window的DataContext绑定ViewModel

    1.全局的ViewModel绑定: a)设定全局的ViewModel(App.xaml中): 1 <Application x:Class="MyTest.App" 2 xm ...

  4. Postgresql命令行和数据库备份与恢复

    进入Postgresql   1 进入数据库   默认安装会创建postgres 用户,,使用postgres用户,psql命令会直接进入数据库: Bash代码   $ su postgres     ...

  5. SQL Server中查询用户的对象权限和角色的方法

    --SQL Server中查询用户的对象权限和角色的方法 -- 查询用户的object权限 exec sp_helprotect NULL, 'sa' -- 查询用户拥有的role exec sp_h ...

  6. iOS-系统自带navigationController-最全设置

    // 导航栏背景色 self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; // 设置push出来的 ...

  7. UILabel的相关属性设置

    在iOS编程中UILabel是一个常用的控件,下面分享一下UILabel的相关属性设置的方法. 很多学习iOS6编程都是从storyboard开始,用到UILabel时是将控件拖到storyboard ...

  8. ASP.NET Web API 2 对 CORS 的支持

    CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览器施加的同域策略安全限制. 所谓同域策略, ...

  9. flask环境配置

    1.首先要看装python环境,最好是python 2.7版本的: 2.安装easy_install,至今也不确定这个东西是怎么装的,我先下载了一个ez_setup,用命令“python ez_set ...

  10. easyui中jquery重复引用问题(tab内存泄露问题)

    今天认真测试了下easyui的一些兼容性问题,发现在ie9 ie10 首次加载的时候,会出现如下bug.如图所示: 认真看了下,我估计是脚本重复引用的问题,我用的iframe框架的模式.登陆系统后,桌 ...