Fence Repair(POJ 3253)

  • 原题如下:
    Fence Repair
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 60998 Accepted: 20117

    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).
  • 题解:首先,切割的方法可以用二叉树来描述,例如:
    复制代码
                                                           15
                                                         /    
                                                        7      8
                                                       /     / 
                                                      3   4  5   3
                                                                / 
                                                               1   2
    复制代码

     这里每一个叶子节点就对应了切割出的一块块木板,叶子节点的深度就对应了为了得到对应木板所需的切割次数,开销的合计就是各叶子节点的"木板的长度*节点的深度"的总和。(这题其实就是裸的哈夫曼树→_→)于是,此时的最佳切割方法首先应该具有这样的性质:最短的板和次短的板的节点应当是兄弟节点。对于最优解来讲,最短的板应当是深度最大的叶子节点之一,所以与这个叶子节点同一深度的兄弟节点一定存在,并且由于同样是最深的叶子节点,所以应该对应于次短的板。不妨将Li从小到大排序,最短的是L1,次短的是L2,它们在二叉树里是兄弟节点,它们是从一块长度为(L1+L2)的板切割来的,由于切割顺序是自由的,不妨当作是最后一次切割,则这次切割前有(L1+L2)、L3、L4、…、Ln,这样的n-1块木板存在,按照相同的方式,将这n-1块木板进行处理,就可以得到整个问题的答案了,复杂度是O(N2)的,使用堆/优先队列优化后可以达到O(nlogn)的复杂度。

  • 代码:
    O(n2)的:
     1 #include <iostream>
     2 using namespace std;
     3 
     4 int n;
     5 int *l;
     6 
     7 int main()
     8 {
     9     cin >> n;
    10     l = new int[n];
    11     for (int i=0; i<n; i++)
    12     {
    13         cin >> l[i];
    14     }
    15     long long ans=0;
    16     while (n>1)
    17     {
    18         int min1=0,min2=1;
    19         if (l[min1]>l[min2])
    20         {
    21             min1=1;
    22             min2=0;
    23         }
    24         for (int i=2; i<n; i++)
    25         {
    26             if (i[l]<min1[l])
    27             {
    28                 min2=min1;
    29                 min1=i;
    30             }
    31             else if(i[l]<min2[l]) min2=i;
    32         }
    33         int t=min1[l]+min2[l];
    34         ans += t;
    35         if (min1==n-1)
    36         {
    37             min1=min2;
    38             min2=n-1;
    39         }
    40         min1[l]=t;
    41         l[min2]=l[n-1];
    42         n--;
    43     }
    44     cout << ans << endl;
    45 }

     O(nlogn)的:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <functional>
     4 #include <queue>
     5 
     6 using namespace std;
     7 
     8 const int MAX_N=20200;
     9 int n;
    10 int l[MAX_N-1];
    11 
    12 int main()
    13 {
    14     cin >> n;
    15     for (int i=0; i<n; i++) cin >> l[i];
    16     long long ans=0;
    17     priority_queue<int, vector<int>, greater<int>> que;
    18     for (int i=0; i<n; i++)
    19     {
    20         que.push(l[i]);
    21     }
    22     while (que.size()>1)
    23     {
    24         int l1,l2;
    25         l1=que.top();
    26         que.pop();
    27         l2=que.top();
    28         que.pop();
    29         ans+=l1+l2;
    30         que.push(l1+l2);
    31     }
    32     cout << ans << endl; 
    33 }
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9416640.html