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 should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .
(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 should 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 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
贪心基础上加个id递增的条件
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<vector> 5 #include<cstring> 6 #include<string> 7 #include<algorithm> 8 #include<map> 9 #include<cmath> 10 #include<math.h> 11 using namespace std; 12 13 struct node 14 { 15 int l,w; 16 }a[5005]; 17 18 bool vis[5005]; 19 20 bool cmp(node b,node c) 21 { 22 if(b.l==c.l) 23 return b.w<c.w; 24 else 25 return b.l<c.l; 26 } 27 28 int main() 29 { 30 31 int T; 32 scanf("%d",&T); 33 while(T--) 34 { 35 int n; 36 scanf("%d",&n); 37 for(int i=0;i<n;i++) 38 scanf("%d%d",&a[i].w,&a[i].l); 39 sort(a,a+n,cmp); 40 int res=0; 41 memset(vis,false,sizeof(vis)); 42 int tmpl,tmpw; 43 for(int i=0;i<n;i++) 44 { 45 if(!vis[i]) 46 { 47 vis[i]=true; 48 res++; 49 tmpl=a[i].l; 50 tmpw=a[i].w; 51 for(int j=i+1;j<n;j++) 52 { 53 if(a[j].l>=tmpl&&a[j].w>=tmpw&&!vis[j]) 54 { 55 vis[j]=true; 56 tmpl=a[j].l; 57 tmpw=a[j].w; 58 } 59 } 60 } 61 } 62 printf("%d\n",res); 63 } 64 return 0; 65 }