Wooden sticks
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11694 accepted submission (s): 4837
Problem descriptionthere 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 (), and ), then the minimum setup time shocould be 2 minutes since there is a sequence of pairs ).
Inputthe 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 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.
Outputthe output shoshould contain the minimum setup time in minutes, one per line.
Sample input3 5 4 9 5 2 2 1 3 5 1 4 32 2 1 1 2 23 1 3 2 3 1
Sample output213 question: give you n wooden sticks, each of which has a length of L and a weight of W. Then, use a machine to process the wooden sticks. The time for processing the first wooden stick is 1, if the I-th wooden rod is processed, then the J-th wooden rod is processed, if l [J]> = L [I] & W [J]> = W [I], no time is required to process the J wooden rod. The shortest time required for processing n wooden sticks. Ideas:
I want to do the conversion question. I read the HDU question classification, but I didn't want to dispatch the conversion equation. I used it to check the dataset and greedy water. Then I went through it... = .. This question clearly leads to the longest chain of the wooden rod. One chain is that the L and W of the front wooden rod I are less than or equal to the L and W of the back wooden rod J. When the chain is longest, the number of links is the minimum, so the time consumed is the shortest. How can we make the chain the longest? We need to be greedy, first sort l from small to large, and then sort by W from small to large, then, when both the L and W of the J sticks are greater than or equal to the L and W of the I sticks, they are combined into a set. Finally, calculate several sets as time. Code:
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <vector> 5 #include <iostream> 6 using namespace std; 7 #define N 5005 8 9 struct node{10 int id;11 int l, w;12 int flag;13 }a[N];14 15 int father[N];16 int findroot(int p){17 int r=p;18 while(r!=father[r]) r=father[r];19 return r;20 }21 22 bool cmp(node a,node b){23 if(a.l==b.l) return a.w<b.w;24 return a.l<b.l;25 }26 27 main()28 {29 int t;30 int n, i, j, ans, k;31 cin>>t;32 while(t--){33 scanf("%d",&n);34 for(i=0;i<=n;i++) father[i]=i;35 k=0;36 for(i=0;i<n;i++) {37 scanf("%d %d",&a[i].l,&a[i].w);38 a[i].id=k++;39 a[i].flag=0;40 }41 sort(a,a+n,cmp);42 for(i=0;i<n;i++){43 if(!a[i].flag){44 k=i;45 for(j=i+1;j<n;j++){46 if(!a[j].flag&&a[j].l>=a[k].l&&a[j].w>=a[k].w){47 father[a[j].id]=a[k].id;48 a[j].flag=1;49 k=j;50 }51 }52 }53 54 }55 ans=0;56 for(i=0;i<n;i++) {57 if(findroot(a[i].id)==a[i].id)58 ans++;59 }60 printf("%d\n",ans);61 }62 }