Wooden Sticks POJ - 1065

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 ) .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

题意:给n个带有长度和重量属性的木棒。处理第一根木棒花费时间为1,其后如果处理的木棒的长度和重量分别大于上一根已经处理的木棒的长度和重量,就不用花时间,否则花费时间加一。
还是仔细读一下题吧。
题解:排完序后处理方式就明显了,贪心,每次从当前位置理到尾,并标记已经处理过的木棒,那么当更新当前位置时,如果没有处理,则花费时间加1.(为什么贪心一定正确,其实在没有排序
的时候,那么处理到比当前长度或者重量大的时候,可以把这跟木棒插入到适合处理它的位置,所以排序后贪心就一定能得到最小值)
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn=5005;
 8 
 9 struct node{
10     int l,w;
11     bool operator<(const node& i)const{
12         if(l==i.l) return w<i.w;
13         return l<i.l;
14     }
15 }a[maxn];
16 
17 int n;
18 bool vis[maxn];
19 
20 void solve()
21 {   memset(vis,0,sizeof(vis));
22     int ans=0;
23     for(int i=1;i<=n;i++){
24         if(vis[i]) continue;
25         ans++;
26         int teml=a[i].l,temw=a[i].w;
27         for(int j=i+1;j<=n;j++){
28             if(vis[j]) continue;
29             if(a[j].l>=teml&&a[j].w>=temw){
30                 teml=a[j].l;
31                 temw=a[j].w;
32                 vis[j]=true;
33             }
34         }
35         vis[i]=true;
36     }
37     cout<<ans<<endl;
38 }
39 
40 int main()
41 {   int cases;
42     cin>>cases;
43     while(cases--){
44         cin>>n;
45         for(int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].w);
46         sort(a+1,a+n+1);
47         solve();
48     }
49 }

 

posted @ 2017-08-04 22:47  天之道,利而不害  阅读(256)  评论(0编辑  收藏  举报