Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1065
Solution report:
This article uses a greedy algorithm. First, find the stick with the smallest length from the heap of sticks, find the stick with the same length, find the smallest weight, and set the length to L and the weight to W. Then, find the stick whose length and weight are greater than or equal to L and W in the remaining sticks, and find the stick with the minimum length and weight. If it does not exist, then, you can directly find the stick with the minimum length and weight, and the time is + 1.
Next, we will prove the feasibility of the greedy algorithm.
The first step proves that a set of optimal solutions can always be obtained through greedy strategy.
Set this question to an optimal solution () (L, W )()
If (L, W) is the smallest stick, then l and W must be smaller than l 'and W' of the previously called stick, that is, when the stick is called, set up time must be added. Therefore, moving (L, W) to the front will not reduce or increase the set up time to call this stick. Then, all the values of ''and W' are greater than or equal to L, and the values of'' and W' in sticks of W are the smallest stick. If it is in the original optimal solution, set up time is not required, after moving it to the stick (L, W) minimum, it will not be reduced or set up time will not be increased. If it needs to calculate the set up time in the original optimal solution, it must not be the optimal solution. Otherwise, after moving it to (L, W), a set up time can be reduced. In turn, we can get a certain optimal solution through greedy strategy.
Step 2: It is proved that the subproblem also requires the optimal solution. Obviously, if the subproblem is not the optimal, the solution of the total problem is certainly not the optimal. Therefore, the subproblem also requires the optimal solution.
# Include <iostream> <br/> using namespace STD; </P> <p> int * l, * w; <br/> int CNT; </P> <p> # define INF 1000000 </P> <p> int min (INT begin, int end, int PL, int PW) // find the values of L and W from L [begin], W [begin] to L [end], and W [end] are greater than Pl, position of the minimum value of l in Pr <br/> //. If no value exists, the location of the minimum value of L is directly returned. <br/>{< br/> int minl = inf, minw = inf; <br/> int locate =-1; <br/> for (INT I = begin; I <end; I ++) <br/>{< br/> If (L [I]> = pl & W [I]> = pw) <br/>{< br/> If (minl> L [I]) <br/>{< br/> minl = L [I]; <br/> minw = W [I]; <br/> locate = I; <br/>}< br/> else if (minl = L [I]) <br/>{< br/> If (minw> W [I]) <br/>{< br/> minw = W [I]; <br/> locate = I; <br/>}< br/> If (locate =-1) <br/>{< br/> CNT ++; <br/> for (INT I = begin; I <end; I ++) <br/>{< br/> If (minl> L [I]) <br/>{< br/> minl = L [I]; <br/> minw = W [I]; <br/> locate = I; <br/>}< br/> else if (minl = L [I]) <br/>{< br/> If (minw> W [I]) <br/>{< br/> minw = W [I]; <br/> locate = I; <br/>}< br/> return locate; <br/>}</P> <p> void Exchange (int I, Int J) <br/>{< br/> int temp1 = L [I]; <br/> int temp2 = W [I]; <br/> W [I] = W [J]; <br/> W [J] = temp2; <br/> L [I] = L [J]; <br/> L [J] = temp1; <br/>}</P> <p> int main () <br/>{< br/> int N; <br/> CIN> N; <br/> while (n --) <br/>{< br/> int m; <br/> CIN> m; <br/> L = new int [m]; <br/> W = new int [m]; <br/> CNT = 1; <br/> for (INT I = 0; I <m; I ++) <br/> scanf ("% d", & L [I], & W [I]); <br/> int PL = 0, PW = 0; <br/> for (INT I = 0; I <m; I ++) <br/>{< br/> int T = min (I, m, PL, PW); <br/> PL = L [T]; <br/> PW = W [T]; <br/> Exchange (I, T); <br/>}< br/> Delete [] L; <br/> Delete [] W; <br/> cout <CNT <Endl; <br/>}< br/> return 0; <br/>}
Appendix:
Wooden sticks
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:10697 |
|
Accepted:4337 |
Description
There is a pile of N wooden sticks. the length and weight of each stick are known in advance. the sticks are to be processed by a woodworking machine in one by one fashion. it needs some time, called setup time, for the machine to prepare processing a stick. the setup times are associated with cleaning operations and changing tools and shapes in the machine. the setup times of the woodworking machine are given as follows:
(A) The setup time for the first wooden stick is 1 minute.
(B) Right after processing a stick of length L and weight W, the machine will need no setup time for a stick of length l' and weight W' if l <= l' and W <= W '. otherwise, it will need 1 minute for setup.
You are to find the minimum setup time to process a given pile of N wooden sticks. for example, if you have five sticks whose pairs of length and weight are (9, 4), (2, 5), (1, 2), (5, 3 ), and (4, 1), then the minimum setup time shocould be 2 minutes since there is a sequence of pairs (4, 1), (5, 3), (9, 4), (1, 2), (2, 5 ).
Input
The input consists of T test cases. the number of test cases (t) is given in the first line of the input file. each test case consists of two lines: the first line has an integer N, 1 <= n <= 5000, that represents the number of wooden sticks in the test case, and the second line contains 2n positive integers L1, W1, L2, W2 ,..., ln, Wn, each of magnloud at most 10000, where Li and WI are the length and weight of the I th wooden stick, respectively. the 2n integers are delimited by one or more spaces.
Output
The output shoshould contain the minimum setup time in minutes, one per line.
Sample Input
3 5 4 9 5 2 2 1 3 5 1 4 3 2 2 1 1 2 2 3 1 3 2 2 3 1
Sample output
213