题目

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

//这道题教我们优先队列的一个用法:取前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. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

  2. DB2执行脚本

    经常会遇到数据库脚本放在.sql文件中,那么怎么去执行这个脚本,而不需要将脚本中的东西粘贴出来再数据库链接工具中执行呢? 下面是DB2数据库脚本执行的办法 环境介绍: 脚本文件名:Script.sql ...

  3. [JAVA][RCP] Eclipse4/RCP/Lifecycle

    E4AP provides two levels of lifecycles, for contributions and for the application. Contents [hide]  ...

  4. Python: 函数参数小结

    参数的类型: 函数的参数有2种类型: 1. 函数定义时用于接收值的形式参数Parameters. 2. 函数调用时用于传递值的实际参数Arguments. 参数的传递: 传递方式有2种: 1. 值传递 ...

  5. python中的各种排序

    #encoding=utf-8 import random from copy import copy def directInsertSort(seq): """ 直接 ...

  6. mysql数据库编码设置成utf-8,避免出现乱码

    设置默认编码为utf8:set names utf8;设置数据库db_name默认为utf8:ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 C ...

  7. 在HTML中添加目录

    <a href="#num1">跳转到第一章</a><div id="num1">第一章</div>用a的hre ...

  8. Swift function how to return nil

    这两天在学习Stanford出品的iOS7的课程,这个课程去年也看过,但是看到第3课就不行了,满篇的OC,把人都搞晕了.这段时间因为要写个iOS的App,正好赶上了Swift问世,所以趁着这股劲继续学 ...

  9. win8.1上安装vc6

    win8.1上安装vc6 1.以管理员方式运行SETUP.EXE,然后一路下一步 2.这里需要一点点耐心,等10分钟左右就能过去,电脑会比较卡,有点像假死,还是没有死掉,等等就好了 3.这里选择vc6 ...

  10. 008--VS2013 C++ 位图半透明化(另一种显示)

    注:主要变化是在下面这张位图上的操作 //全局变量HBITMAP bg, girl;HDC mdc;//起始坐标const int xstart = 50;const int ystart = 20; ...