POJ 3253 Fence Repair (贪心)】的更多相关文章

Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3253 Appoint description:  hanjiangtao  (2014-11-12) System Crawler  (2015-04-24) Description Farmer John wants to repair a small len…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深度相关的 由于切割的次数是确定的 该二叉树的节点就是确定的. 也就是说我们可以贪心的处理  最小长度的子节点放在最下面 如图 ac代码如下 使用了堆 记录每次最小的元素 堆的使用真的不是很方便 , 另外还需要注意 爆int 所以需要使用long long 记录元素的和 #include <iostrea…
题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目所求花费一致.贪心,按木板长度排序,每次取长度最小的两块木板,则答案最小.因为合成次数是固定不变的,尽量让小的部分进行多次合成,这样总花费最小.   #include<cstdio> #include<queue> using namespace std; typedef long l…
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, eac…
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the…
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) u…
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23913   Accepted: 7595 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个元素为止. 代码例如以下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <c…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19660   Accepted: 6236 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53106   Accepted: 17508 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)…
Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a s…
题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切割要求画出二叉树. 总开销 = 各叶子节点的值 x 该叶子节点的深度 树的深度一定,为了使总开销尽可能的小,那么比较深的叶子节点的值应尽可能的小,所以二叉树应为: 模拟出所建立的二叉树,求出根节点的值即可 为了节省时间,可以利用优先队列每次取出最小的两个值合并,并将两数之和重新加入数组,直到数组内的…
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3253" rel="nofollow">http://poj.org/problem?id=3253 Description Farmer John wants to repair a s…
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<iomanip> #include<cmath> #include<…
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个要求的小木板的长度,及小木板的个数n,求最小费用 以 3 5 8 5为例: 先从无限长的木板上锯下长度为 21 的木板,花费 21 再从长度为21的木板上锯下长度为5的木板,花费5 再从长度为16的木板上锯下长度为8的木板,花费8 总花费 = 21+5+8 =34 解题思路:哈夫曼编码模板 代码:…
Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a sin…
题目链接: http://poj.org/problem?id=3253 题目大意: 有一根木棍,需要截成n节,每节都有固定的长度,一根长度为x的木棒结成两段,需要花费为x,问截成需要的状态需要最小的花费? 解题思路: 哈夫曼数,把每节需要的木棒长度看做树上的节点,把截木棍的过程倒过来,变成把n截木棍接起来,这两个过程的花费是一样的.根据哈夫曼的性质,可知先把最短的两个木棍连起来后,放到剩下的n-2根木棍中,再选取两个最短的连接起来,再放回去,直到全部的木根都连在一起就ok了. 代码: #inc…
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割前的长度. 有个陷阱就是需要用long long 去储存 如 Sample Input 3 8 5 8 Sample Output 34 就是将一块长为21的木板先切成13和8,花费21.然后将13切成5和8,花费13,总花费21 + 13 = 34. 分析:如果考虑从一块木板分割成小木板,方法数会…
题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用于取前2个小的元素 #include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; _…
题意:给出n根木板,需要把它们连接起来,每一次连接的花费是他们的长度之和,问最少需要多少钱. 和上一题果子合并一样,只不过这一题用long long 学习的手写二叉堆的代码,再好好理解= = #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map>…
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价. 思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列. 注意priority_queue的用法,原型: priority_queue<Type> q; priority_queue<Type,deque<Type>,Comp> q; 其中Type是类型,Comp是比较结构体,比较函数是它的括号重载,比如对int型从小到大排序的Comp结构体如下: s…
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1次,如果把每根木棍最初的长度想成叶子节点,两个节点的父节点就是这两个节点的长度相加.那么求的就是这n-1个父节点的总和最少. 为实现这个目标,我们使每个父节点的值尽可能的少,由此想到哈夫曼编码的原理.初始条件为一共n根木棍.然后从中选出最短的两根,将这两根拼接成一根,这样得到的父节点的值是最小的.然…