优先队列 poj3253 Fence Repair

Posted 木子丘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了优先队列 poj3253 Fence Repair相关的知识,希望对你有一定的参考价值。

Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 51411   Accepted: 16879

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条小木块,每一块长度为len的木板需要len的费用,求切完所有木板的费用之和的最小值

解题思路:本题的解法类似于哈夫曼编码,一直选用两块最小的相加,得到费用之和,本题用到了优先队列,本题要注意两点1.单独判断木板只有一个的情况,2.输出时费用之和要用long long类型的,否则会wrong answer

本题代码:

 1 #include<queue>
 2 #include<vector>
 3 #include<stdio.h>
 4 #include<iostream>
 5 using namespace std;
 6 int n;
 7 long long sum;
 8 int main(){
 9     while(~scanf("%d",&n)){
10         sum=0;
11         priority_queue<int,vector<int>,greater<int> >q;
12         for(int i=0;i<n;i++){
13             int a;
14             scanf("%d",&a);
15             q.push(a);
16         }
17         if(q.size()==1){
18             sum=q.top();
19             q.pop();
20         }
21         while(q.size()>1){
22            int m,t;
23            m=q.top();
24            sum+=m;
25            q.pop();
26            t=q.top();
27            sum+=t;
28            q.pop();
29            q.push(m+t);
30         }
31         printf("%lld\n",sum);
32     }
33     return 0;
34 }

 

笔记:优先队列

三种用法
第一种,直接使用默认的:
它的模板声明带有三个参数,priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式.
Container 必须是用数组实现的容器,比如 vector, deque(双向队列) 但不能用 list(链表).
STL里面默认用的是 vector. 比较方式默认用 operator< .
所以如果你把后面俩个参数缺省的话,优先队列就是大顶堆,队头元素最大.

priority_queue<int>Q;
Q.push(1);
Q.push(2);
Q.push(4);
Q.push(3);
cout<<Q.size()<<"\n";
while(!Q.empty())
{
  cout<<Q.top()<<" ";
  Q.pop();
}

输出:
4
4 3 2 1

第二种方法:(从小到大输出)
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
priority_queue<int, vector<int>, greater<int> >Q

priority_queue<int, vector<int>, greater<int> >Q;
Q.push(1);
Q.push(2);
Q.push(4);
Q.push(3);
cout<<Q.size()<<"\n";
while(!Q.empty())
{
  cout<<Q.top()<<" ";
    Q.pop();
}

输出:
4
1 2 3 4

第三种,自定义类型:
重载 operator < 或者自己写仿函数

struct Node
{
  int x;
  int y;
  Node() {};
  Node(int a,int b)
  {
  x=a;
  y=b;
  }
};

bool operator < (Node a,Node b)
{
  if(a.x==b.x)
  return a.y>b.y;
  return a.x>b.x;
}

int main()
{
  priority_queue<Node>q;
  q.push(Node(1,2));
  q.push(Node(1,1));
  q.push(Node(5,6));
  q.push(Node(3,4));
  q.push(Node(2,7));
  while(!q.empty())
  {
    Node node=q.top();
    q.pop();
    cout<<node.x<<" "<<node.y<<"\n";
  }
  return 0;
}

输出:

1 1
1 2
2 7
3 4
5 6

也可以这么写

struct Node
{
  int x;
  int y;
  Node() {};
  Node(int a,int b)
  {
    x=a;
    y=b;
  }
const bool operator < (Node b)const
{
    if(x==b.x)
  return y>b.y;
  return x>b.x;
  }
};

还有重载()运算符

struct Node
{
  int x;
  int y;
  Node() {};
  Node(int a,int b)
  {
    x=a;
    y=b;
  }
};
struct cmp
{
  bool operator () (Node a,Node b)
  {
  if(a.x==b.x)
  return a.y>b.y;
  return a.x>b.x;
  }
};

 

以上是关于优先队列 poj3253 Fence Repair的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3253 Fence Repair 贪心 优先级队列

poj3253 Fence Repair哈夫曼树+优先队列

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

poj 3253 fence repair

POJ 3253 Fence Repair

POJ-3253 Fence Repair---Huffman贪心