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

明显的huffman树,优先队列是个很好的东西。

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <iostream>
#include <vector>
#define ll __int64
using namespace std; int n;
struct cmp //优先级重载
{
bool operator()(ll x, ll y)
{
return x > y;
}
}; priority_queue<ll, vector<ll>, cmp> q; int main()
{
ll x;
while (scanf("%d", &n) != EOF)
{
while (!q.empty()) q.pop();
ll sum = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
sum += x;
q.push(x);
}
ll ans = 0;
// while (!q.empty()) {printf("%d ", q.top()); q.pop();}
while (!q.empty())
{
ll a = q.top();
q.pop();
ll b = q.top();
q.pop();
ans += a+b;
x = a+b;
if (!q.empty())
q.push(x);
}
cout << ans << endl;
}
return 0;
}

poj 3253 Fence Repair(优先队列+huffman树)的更多相关文章

  1. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

  2. poj 3253 Fence Repair(模拟huffman树 + 优先队列)

    题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_ ...

  3. poj 3253 Fence Repair 优先队列

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

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

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

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

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

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

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

  7. POJ 3253 Fence Repair(修篱笆)

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

  8. poj 3253 Fence Repair(优先队列+哈夫曼树)

    题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...

  9. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

随机推荐

  1. 怎么安装IDEA?

    我们使用的是的2017.3.4版本: 然后弹出激活窗口 破解方法: 1.下载破解插件JetbrainsCrack-2.7-release-str.jar 2.把插件放到安装目录下的bin文件夹下 3. ...

  2. 长春理工大学第十四届程序设计竞赛(重现赛)F

    F. Successione di Fixoracci 题目链接:https://ac.nowcoder.com/acm/contest/912/F 题目: 动态规划(Dynamic programm ...

  3. Asp.net HttpClient Proxy(Fiddler)

    <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault=& ...

  4. TCP/IP 第四、五章

    1, 2, 整个arp请求的过程. 3,arp -a 获取arp高速缓存.一般arp高速缓存存活时间20分钟,不完整的表项设置为3分钟.因为机器的ip地址可能发生改变. 4, 5,arp一般是操作系统 ...

  5. 常用的方法论-PARR

  6. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  7. scrapy基础知识之 parse()方法的工作机制思考:

    1.因为使用的yield,而不是return.parse函数将会被当做一个生成器使用.scrapy会逐一获取parse方法中生成的结果,并判断该结果是一个什么样的类型: 2.如果是request则加入 ...

  8. python3 连接数据库注意点

    类库:pymysql ''' Created on 2019年 @author: Root ''' import pymysql from name import getName # 数据库连接信息 ...

  9. java中String和int相互转换常用方法详解

    java中int和String的相互转换常用的几种方法: String  > int s="10";int i;第一种方法:i=Integer.parseInt(s);//默 ...

  10. EPG开发《异常排查以及解决方案》

    [框架]