题目链接:

https://vjudge.net/problem/POJ-3253

题目大意:

有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度

给定各个要求的小木板的长度,及小木板的个数n,求最小费用

思路:

HUffman算法

优先队列

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
int n;
int main()
{
while(scanf("%d", &n) != EOF)
{
priority_queue<int, vector<int>, greater<int> >q;
int x;
for(int i = ; i < n; i++)
{
scanf("%d", &x);
q.push(x);
}
ll ans = ;
while(q.size() > )
{
int a = q.top();
q.pop();
int b = q.top();
q.pop();
q.push(a + b);
ans += (a + b);
}
cout<<ans<<endl;
}
return ;
}

POJ-3253 Fence Repair---Huffman贪心的更多相关文章

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

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

  2. poj 3253 Fence Repair (贪心,优先队列)

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

  3. POJ 3253 Fence Repair (贪心)

    题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切 ...

  4. POJ 3253 Fence Repair(修篱笆)

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

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

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

  6. poj 3253 Fence Repair 优先队列

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

  7. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  8. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

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

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

  10. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

随机推荐

  1. 剑指Offer-孩子们的游戏(圆圈中最后剩下的数)

    package Other; import java.util.LinkedList; /** * 孩子们的游戏(圆圈中最后剩下的数) * 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友, ...

  2. Httpclient post请求

    CloseableHttpClient httpclient = HttpClients.createDefault(); String url = "https://ml.yiche.co ...

  3. ng-if ng-show ng-hide区别(面试题)

    ng-if ng-show  ng-hide区别 实现原理方面: ng-show/ng-hide是通过修改css样式方式控制元素显示与隐藏,对应的DOM元素会一直存在于当前页面中: 而ng-if根据表 ...

  4. 【最全】经典排序算法(C语言)

    算法复杂度比较: 算法分类 一.直接插入排序 一个插入排序是另一种简单排序,它的思路是:每次从未排好的序列中选出第一个元素插入到已排好的序列中. 它的算法步骤可以大致归纳如下: 从未排好的序列中拿出首 ...

  5. Java多线程:CopyOnWrite容器

    一.什么是CopyOnWrite容器 CopyOnWrite容器即写时复制的容器.通俗的理解是当我们往一个容器添加元素的时候,不直接往当前容器添加,而是先将当前容器进行Copy,复制出一个新的容器,然 ...

  6. fail2ban 防止ssh暴力破解

    1.环境 CentOS 7 2.在线安装 yum install -y epel-release yum install -y fail2ban fail2ban 结构 /etc/fail2ban   ...

  7. 动态规划(Dynamic programming) 走楼梯

    来自:算法爱好者 有一座高度是10级台阶的楼梯,从下往上走,每跨一步只能向上1级或者2级台阶,要求用程序来求出一共有多少种走法? f(10) = f(9) + f(8) f(9) = f(8) + f ...

  8. Linux下C编写基本的多线程socket服务器

    不想多说什么,会搜这些东西的都是想看代码的吧. 一开始不熟悉多线程的时候还在想怎么来控制一个线程的结束,后来发现原来有pthread_exit()函数可以直接在线程函数内部调用结束这个线程. 开始还想 ...

  9. cpp常用函数总结

    //sprintf sprintf(temp_str_result, "%lf", temp_double); result = temp_str_result; (*begin) ...

  10. 在arc模式下 CGImage 释放问题

    //大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...