题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051
Wooden Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13534 Accepted Submission(s):
5590
Problem 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 (4,9), (5,2), (2,1), (3,5), and
(1,4), then the minimum setup time should be 2 minutes since there is a sequence
of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
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 n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude 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 should 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
2
1
3
题目大意:一堆木棍,有长度和重量,处理第一根需要一分钟,然后处理下一根木棍,如果这根木棍的长度和重量都不大于之前的木棍,则不需要花费时间,否则也需要一分钟。
分析:首先排序,先按长度排序,如果长度一样,则按重量排序(从小到大和从大到小排序都可以,我是按从小到大排序的)。然后求递增(减)序列的最小个数。
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int T,n; 9 int a[5005],b[5005],vis[5001]; 10 11 int main () 12 { 13 int i,j; 14 int time; 15 int x,y; 16 scanf ("%d",&T); 17 while (T--) 18 { 19 scanf ("%d",&n); 20 for (i=0; i<n; i++) 21 scanf ("%d %d",&a[i], &b[i]); 22 for (i=0; i<n; i++)//两个for循环排序,没用结构体,比较麻烦 23 { 24 for (j=i; j<n; j++) 25 { 26 if (a[i] > a[j]) 27 { 28 swap(a[i], a[j]); 29 swap(b[i], b[j]); 30 } 31 if (a[i] == a[j]) 32 { 33 if (b[i] > b[j]) 34 { 35 swap(a[i], a[j]); 36 swap(b[i], b[j]); 37 } 38 } 39 40 } 41 } 42 time = 0; 43 memset(vis, 0, sizeof(vis)); 44 for (i=0; i<n; i++) 45 { 46 if (vis[i])//若该木棍被访问过,跳过 47 continue; 48 //x = a[i]; 49 y = b[i]; 50 for (j=i+1; j<n; j++) 51 { 52 if (!vis[j] && b[j] >= y) 53 { 54 55 vis[j] = 1;//标记访问 56 //x = a[j]; 57 y = b[j];//当前最大重量 58 } 59 } 60 time++;//时间加一 61 } 62 printf ("%d\n",time); 63 } 64 return 0; 65 }