zoukankan      html  css  js  c++  java
  • 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 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).
     
    题解:要使总费用最小 那么每次只选取最小长度的两块木板相加 再把这些“和”累加到总费用中即可(哈夫曼)
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 const int N=20005;
    29 const int mod=1e9+7;
    30 int a[N];
    31 int main()
    32 {
    33     std::ios::sync_with_stdio(false);
    34     ll s;
    35     int i,n,t,a,b;
    36     cin>>n;
    37     priority_queue<int,vector<int>,greater<int> >q;
    38     for(i=0;i<n;i++){
    39         cin>>t;
    40         q.push(t);
    41     }
    42     s=0;
    43     if(q.size()==1){
    44         a=q.top();
    45         s+=a;
    46         q.pop();
    47     }
    48     while(q.size()>1){
    49         a=q.top();
    50         q.pop();
    51         b=q.top();
    52         q.pop();
    53         t=a+b;
    54         s+=t;
    55         q.push(t);
    56     }
    57     cout<<s<<endl;
    58     return 0;
    59 }
  • 相关阅读:
    Eclipse中jsp、js文件编辑时,卡死现象解决汇总
    环境安装备忘录 Nginx
    环境安装备忘录 Redis redis-slave-端口号.conf
    环境安装备忘录 Redis redis-common.conf
    环境安装备忘录 Redis
    Leetcode 131 Palindrome Partitioning(DFS思路)
    排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors
    谈STL的重要应用与实现
    二分图匹配相关问题
    理解操作系统相关知识
  • 原文地址:https://www.cnblogs.com/wydxry/p/7272547.html
Copyright © 2011-2022 走看看