poj 3253 Fence Repair

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) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn‘t own a saw with which to cut the wood, so he mosies over to Farmer Don‘s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn‘t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks 
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

大致题意:

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

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

提示:

3

5 8 5为例:

先从无限长的木板上锯下长度为 21 的木板,花费 21

再从长度为21的木板上锯下长度为5的木板,花费5

再从长度为16的木板上锯下长度为8的木板,花费8

总花费 = 21+5+8 =34

解题思路:

利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可

因为朴素的HuffmanTree思想是:

(1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用

(2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,则累计的费用就是最小费用

HuffmanTree超时的原因是每次都要重新排序,极度浪费时间,即使是用快排。

一个优化的处理是:

(1)只在输入全部数据后,进行一次升序排序  (以后不再排序)

(2)队列指针p指向队列第1个元素,然后取出队首的前2个元素,把他们的和值累计到总费用,再把和值sum作为一个新元素插入到队列适当的位置

由于原队首的前2个元素已被取出,因此这两个位置被废弃,我们可以在插入操作时,利用后一个元素位置,先把队列指针p+1,使他指向第2个废弃元素的位置,然后把sum从第3个位置开始向后逐一与各个元素比较,若大于该元素,则该元素前移一位,否则sum插入当前正在比较元素(队列中大于等于sum的第一个元素)的前一个位置

(3)以当前p的位置作为新队列的队首,重复上述操作

另一种处理方法是利用STL的优先队列,priority_queue,非常方便简单高效,虽然priority_queue的基本理论思想还是上述的优化思想,但是STL可以直接用相关的功能函数实现这些操作,相对简单,详细参见我的程序。

注意priority_queue与qsort的比较规则的返回值的意义刚好相反

第一种方法:时间复杂度O(N*N)

将数组中最小的两个数放在数组前两个位置,将相加得到的结果和数组的最后一个元素放到数组前两个位置,并且是数组逻辑上减1。知道数组逻辑长度 <= 1.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;

int N, L[20000];

void solve(){
    ll ans = 0;
    if(N == 1){
        ans = L[0];
    }
    while(N > 1){
        int mii1 = 0, mii2 = 1;
        //保持数组第一个最短,第二个次短
        if(L[mii1] > L[mii2])
            swap(L[mii1], L[mii2]);
        for(int i = 2; i < N; i++){
            if(L[i] < L[mii1]){
                swap(L[i], L[mii2]);
                swap(L[mii1], L[mii2]);
            } else if(L[i] < L[mii2]){
                swap(L[i], L[mii2]);
            }
        }
        int t = L[mii1] + L[mii2];
        ans += t;
        L[mii1] = t;
        L[mii2] = L[N-1];
        N--;
    }
    printf("%lld\n", ans);
}    

int main(){
    while(scanf("%d", &N) != EOF){
        for(int i = 0; i < N; i++)
            scanf("%d", &L[i]);
        solve();
    }
    return 0;
}

第二种方法:时间复杂度O(N*logN)

由于只需从板的集合里取出最短的两块,并且把长度为两板块长度之和的板加入集合中即可,因此使用优先队列(原理:用到了堆这个数据结构)就可以高效地实现。一共需要进行O(N)次O(logN)的操作,因此总的时间复杂度是O(N*logN)。(优先队列的插入和弹出即是堆的插入和移除),有最大优先队列和最小优先队列。

这里直接用stl里的优先队列priority_queue(默认是最大优先队列,即取出的是最大值)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 typedef long long ll;
 7 //输入
 8 int N, L[20000];
 9
10 void solve(){
11     ll ans = 0;
12     //声明一个从小到大取出数值的优先队列
13     priority_queue<int, vector<int>, greater<int> > que;
14     for(int i = 0; i < N; i++){
15         que.push(L[i]);
16     }
17     //循环到只剩一块木板为止
18     while(que.size() > 1){
19         int l1, l2;
20         //取出最短的木板和次短的木板
21         l1 = que.top();
22         que.pop();
23         l2 = que.top();
24         que.pop();
25         //把两块木板合并
26         ans += l1 + l2;
27         que.push(l1 + l2);
28     }
29
30     printf("%lld\n", ans);
31 }
32
33 int main(){
34     while(scanf("%d", &N) != EOF){
35         for(int i = 0; i < N; i++)
36             scanf("%d", &L[i]);
37         solve();
38     }
39     return 0;
40 }    

优先队列的比较函数priority_queue与qsort的比较规则的返回值的意义刚好相反

//比较规则,最小优先
class cmp
{
public:
    bool operator()(const int a,const int b)const
    {
        return a>b;
    }
};

priority_queue<int,vector<int>,cmp>Queue;  //定义最小优先队列

priority_queue<int> queue;//默认为最大优先队列

在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:

priority_queue<int> qi;

通过<操作符可知在整数中元素大的优先级高。
故示例1中输出结果为:9 6 5 3 2

第二种方法:
如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

priority_queue<int, vector<int>, greater<int> >qi2;

其中
第二个参数为容器类型。
第二个参数为比较函数。
故示例2中输出结果为:2 3 5 6 9

第三种方法:
自定义优先级。

时间: 2024-08-06 11:39:26

poj 3253 Fence Repair的相关文章

POJ 3253 Fence Repair (优先队列)

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 类似哈夫曼树的贪心思想

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24550   Accepted: 7878 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)

【优先队列/huffman】sdut 2848/poj 3253——Fence Repair

来源:点击打开链接 很久很久之前做过这个题,印象中是用优先队列来做,结果一写各种wa了..........翻之前的代码库,发现优先队列的定义出现了问题.. 因为数据很大需要每次都选取两个最短的进行拼装,所以用了优先队列,每两个小的构成父节点,然后把父节点放进去再找两个小的接起来.huffmanTree的逆向思维,接到最后那一个就是最后的答案了. #include <iostream> #include <queue> #include <vector> #include

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

题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每一个父节点都是两个子节点的和.这个题就是可以从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个元素为止. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <cty

POJ 3253 Fence Repair(哈夫曼树)

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)

哈夫曼树 POJ 3253 Fence Repair

竟然做过原题,一眼看上去竟然没感觉... 哈夫曼树定义:给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近. 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径.通路中分支的数目称为路径长度.若规定根结点的层数为1,则从根结点到第L层结点的路径长度为L-1. 2.结点的权及带权路径长度 若将树中结点赋给一个有着某

POJ 3253 Fence Repair(优先队列,哈夫曼树)

题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用于取前2个小的元素 #include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; _

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

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)

POJ 3253 Fence Repair 贪心 优先级队列

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)