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

本题代码:

 #include<queue>
#include<vector>
#include<stdio.h>
#include<iostream>
using namespace std;
int n;
long long sum;
int main(){
while(~scanf("%d",&n)){
sum=;
priority_queue<int,vector<int>,greater<int> >q;
for(int i=;i<n;i++){
int a;
scanf("%d",&a);
q.push(a);
}
if(q.size()==){
sum=q.top();
q.pop();
}
while(q.size()>){
int m,t;
m=q.top();
sum+=m;
q.pop();
t=q.top();
sum+=t;
q.pop();
q.push(m+t);
}
printf("%lld\n",sum);
}
return ;
}

笔记:优先队列

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

priority_queue<int>Q;
Q.push();
Q.push();
Q.push();
Q.push();
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();
Q.push();
Q.push();
Q.push();
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(,));
  q.push(Node(,));
  q.push(Node(,));
  q.push(Node(,));
  q.push(Node(,));
  while(!q.empty())
  {
    Node node=q.top();
    q.pop();
    cout<<node.x<<" "<<node.y<<"\n";
  }
  return ;
}

输出:

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的更多相关文章

  1. Poj3253 Fence Repair (优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 67319   Accepted: 22142 De ...

  2. poj3253 Fence Repair【哈夫曼树+优先队列】

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

  3. poj-3253 fence repair(贪心题)

    题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...

  4. poj3253 Fence Repair

    http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...

  5. POJ3253 Fence Repair(贪心)

    分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束.这里使用优先队列较为方便. #include<iostream&g ...

  6. poj3253 Fence Repair(贪心+哈夫曼 经典)

    https://vjudge.net/problem/POJ-3253 很经典的题,运用哈夫曼思想,想想很有道理!! 具体实现还是有点绕人,最后被long long卡了一下,看数据大小的时候单纯相乘了 ...

  7. POJ3253 Fence Repair【贪心】

    我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...

  8. [POJ3253]Fence Repair(单调队列)

    题目链接 http://poj.org/problem?id=3253 题目描述 大意:切长度为a的木条的花费是a,给定最终切好的n段各自的长度,问由原来的一根木条(长度为n段长度和)以最终总花费最小 ...

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

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

随机推荐

  1. Leetcode题解(十四)

    39.Combination Sum 题目 题目要求找出和为target的数字组合,并且每个整数可以多次使用.仔细思考可以发现,这道题目可以采用递归的方法来完成,比如举的例子,target=7,一开始 ...

  2. Django开发小型站之前期准备(一)

    语言:python3.5 工具:JetBrains PyCharm virtualenvwrapper优点: 1.使不同的应用开发环境独立 2.环境升级不影响其他应用,也不会影响全局的python环境 ...

  3. 0_Simple__cppOverload

    使用cuda内质结构 cudaFuncAttributes 来观察核函数的共享内存.寄存器数量. ▶ 源代码: /*cppOverload_kernel.cuh*/ __global__ void s ...

  4. progress 相关事件 异步 ajax

    loadstart — Fires when the fi rst byte of the response has been received.progress — Fires repeatedly ...

  5. js实现小球的弹性碰撞。

      前  言 MYBG 小编最近在做自己的个人网站,其中就用到了一个小球碰撞检测的功能,想自己写,无奈本人能力不足啊(毕竟还是一个菜鸟)!!就想着找个插件用一下也好,可是找了好久也没有找到一个比较好用 ...

  6. 一起写框架-MVC框架-基础功能-Date类型数据绑定(七)

    实现功能 表单请求传递的数据,格式为以下格式的日期时间数据. (1):yyyy-MM-dd hh:mm:ss (2):yyyy-MM-dd 执行方法可以使用Date类型接收. 实现思路 1.获得表单字 ...

  7. [转载] Solr使用入门指南

    转载自http://blog.csdn.net/liuzhenwen/article/details/4060922 由于搜索引擎功能在门户社区中对提高用户体验有着重要的作用,在门户社区中涉及大量需要 ...

  8. Request 和 Response 区别

    Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...

  9. 【小技巧解决大问题】使用 frp 突破阿里云主机无弹性公网 IP 不能用作 Web 服务器的限制

    背景 今年 8 月份左右,打折价买了一个阿里云主机,比平常便宜了 2000 多块.买了之后,本想作为一个博客网站的,毕竟国内的服务器访问肯定快一些.满心欢喜的下单之后,却发现 http 服务,外网怎么 ...

  10. 使用JavaMail发送带附件的邮件

    所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...