zoukankan      html  css  js  c++  java
  • POJ 3253 Fence Repair 贪心

                      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) 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块。准备切成的木板的长度为L1,到Ln,
    未切割前的木板的长度刚好的切割后的木板长度的和
    每次切割木板时,需要的开销为这块木板的长度,
    求出最小的开销。
     
    贪心,我刚开始是在想:
    1.每次尽量平均切割成2块
    2.每次把最大的木板切割出去。
     
     
    然后发现都不是最优,
     
    后来想到,可以从后往前推,
     
    先从大到小排序。
    每次合并当前最小的2块木板,并把新合成的木板加进去,然后移动到适当的位置,使木板依然有序。
     
    不断合并,直到只剩下一块木板为止。
     
     
    注意:答案要 long long,不然就wawawawa了。
     
     
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 
     4 using namespace std;
     5 
     6 const int maxn=20000+5;
     7 
     8 int a[maxn];
     9 
    10 bool cmp(int x,int y)
    11 {
    12     return x>y;
    13 }
    14 
    15 int main()
    16 {
    17     int n;
    18 
    19     while(scanf("%d",&n)!=EOF)
    20     {
    21 
    22         for(int i=1;i<=n;i++)
    23             scanf("%d",&a[i]);
    24 
    25         long long ans=0;
    26 
    27         sort(a+1,a+n+1,cmp);
    28 
    29         int cur=n;
    30 
    31         while(cur>1)
    32         {
    33             ans=(long long)(ans+a[cur]+a[cur-1]);
    34 
    35             cur--;
    36 
    37             a[cur]+=a[cur+1];
    38 
    39             int i=cur;
    40 
    41             while(i>1&&a[i]>a[i-1])
    42             {
    43                 swap(a[i],a[i-1]);
    44                 i--;
    45             }
    46         }
    47 
    48         printf("%lld
    ",ans);
    49     }
    50 
    51     return 0;
    52 }
    View Code
     
     
     
     
     
  • 相关阅读:
    tomcat-1
    oscache-2
    oscache-3
    oscache-1
    oscache-4
    缓存概要
    Criterion & DetachedCriteria
    Hibernate <查询缓存>
    Hibernate <二级缓存>
    Hibernate <一级缓存>
  • 原文地址:https://www.cnblogs.com/-maybe/p/4470628.html
Copyright © 2011-2022 走看看