http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117
As in the challenge procedure book, will cut the N-broken board, respectively, corresponding to the leaf node of two tree tree, the total cost of cutting is the length of the Board X-node depth, you can draw understanding, then the shortest plank (L1) should be the depth of the largest leaf node, the second short plate (L2) and it is a brother node, It is cut by a plank of length (L1+L2) so that it becomes a (n-1) piece of wood and then recursively solves the n==1.
The greedy part of the book uses an O (NXN) algorithm that will time out here, requiring a 2.4-section priority queue O (NLOGN) algorithm.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 50005 - using namespacestd; the + intL[n]; A intMain () the { + //Read (); - intN; $ Long Longans=0; $scanf"%d",&n); - for(intI=0; i<n;i++) scanf ("%d",&l[i]); - while(n>1) the { - intm1=0, m2=1;Wuyi if(l[m1]>l[m2]) swap (M1,M2); the for(intI=2; i<n;i++) - { Wu if(l[m1]>L[i]) - { AboutM2=M1; $m1=i; - } - Else if(l[m2]>L[i]) - { AM2=i; + } the } - //printf ("%d%d\n", l[m1],l[m2]); $ intt=l[m1]+l[m2]; theans+=T; the if(m1==n-1) swap (M1,M2); thel[m1]=T; thel[m2]=l[n-1]; -n--; in } theprintf"%lld\n", ans); the return 0; About}
Optimized code: Each time only need to select the minimum length of two pieces of wood, then the priority queue from small to large order, each time the extraction of the two pieces of wood and pressed into the queue.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> - //#include <map> the#include <queue> - - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 50005 - using namespacestd; the + intL[n]; Apriority_queue<int, vector<int, greater<int> >que; the intMain () + { - //Read (); $ intN; $scanf"%d",&n); - for(intI=0; i<n;i++) - { thescanf"%d",&l[i]); - Que.push (L[i]);Wuyi } thell ans=0; - while(Que.size () >1) Wu { - intL1,l2; Aboutl1=que.top (); $ Que.pop (); -L2=que.top (); - Que.pop (); -ans+=l1+L2; AQue.push (l1+L2); + } theprintf"%lld\n", ans); - return 0; $}
51nod 1117 Smart Carpenter (greedy)