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 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
题目大意:
给你n个木棒,每个木棒长度为l、重量为w,然后用机器加工木棒,机器加工第一个木棒时间为1,若加工完第i个木棒后,再加工第j个木棒,若l[j]>=l[i]&&w[j]>=w[i]则加工第j个木棒不需要消耗时间。求加工完n条木棒所需要的最短时间。
思路:
想做动归题目,看了一下HDU题目分类,但是没想出动归转移方程,用并查集和贪心水了一下就过了。。。= = 。。
这道题很明显使得木棒构成的链最长,一条链就是前面木棒i的l和w都小于等于后面木棒j的l和w, 当链最长的时候,那么链的个数最少,于是花费的时间也最短。怎么使得链最长呢,就需要贪心了,先对l从小到大排序,再按w从小到大排序,然后当第j个木棒的l和w都大于等于第i个木棒的l和w的话就把它们并到一个集合里。最后算一下几个集合即为时间。
代码:
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 }
HDU 1051 并查集+贪心,布布扣,bubuko.com