Wooden sticks
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:11794 |
|
Accepted:4808 |
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
Question]
There are n wooden sticks, each of which has different lengths and weights. Use a machine to handle these wooden sticks, when the length of the I + 1 wooden stick> = the length of the I + 1 wooden stick & the weight of the I + 1 wooden stick> = the weight of the I wooden stick, the unit time for processing a wooden stick is set to 1, and the minimum time required for processing n wooden sticks is obtained.
[Question]
Sort by the length of a wooden stick from small to large. When the length of a wooden stick is equal, sort by the weight from small to large. Then, select a wooden stick that is larger than the current length (the wooden sticks have been sorted, and each selection has the least impact on the backend) and mark the selected wooden sticks. After one round, setup time + 1 and then select the remaining wooden sticks to obtain the optimal solution.
# Include <iostream> <br/> using namespace STD; <br/> struct Wood <br/> {<br/> int L; <br/> int W; <br/>} st [5005]; <br/> int work (INT m) <br/>{< br/> int temp; <br/> int sum = 0; <br/> for (INT I = 0; I <m; ++ I) <br/> {<br/> If (ST [I]. w! =-1) <br/>{< br/> temp = sT [I]. w; <br/> sum ++; <br/> for (Int J = I + 1; j <m; ++ J) <br/> {<br/> If (ST [J]. w> = temp) <br/> {<br/> temp = sT [J]. w; <br/> st [J]. W =-1; <br/>}< br/> return sum; <br/>}</P> <p> // comparison function <br/> int CMP (const void * a, const void * B) <br/> {<br/> If (* (wood *) ). L = (* (wood *) B ). l) <br/> return (* (wood *) ). w-(* (wood *) B ). w; <br/> return (* (wood *) ). l-(* (wood *) B ). l; <br/>}</P> <p> int main () <br/>{< br/> int n, m; <br/> CIN> N; <br/> while (n --) <br/>{< br/> CIN> m; <br/> for (INT I = 0; I <m; ++ I) <br/>{< br/> CIN> st [I]. l> st [I]. w; <br/>}< br/> qsort (St, M, sizeof (ST [0]), CMP); <br/> cout <work (m) <Endl; <br/>}< br/> return 0; <br/>}