hdu 1051:Wooden Sticks(贪心)

Wooden Sticks

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


这道题是贪心问题,思路是将木材分堆,每堆的后一个木材的长度l和重量w都比前一个的要大。按这样的规则分出几堆木材,那么最后就要多花几分钟准备。
下面是题目中给出的几组例子的分析:

Min                      1      2   ==>  min=2

4  9        1  4     1

5  2    2      1     0     1

2  1    =>   3  5     1

3  5        4  9     1

1  4        5  2     0     1

 

Min                               1       ==>   min=1

2  2          1  1   1  

1  1   =>    2  2     1

2  2            2  2     1

 

Min                           1    2    3    ==>   min=2

1  3        1  3     1  

2  2    =>   2  2     0    1

3  1            3  1     0    0   1

即先对l排序,然后根据规则对w进行贪心选择,选择出几个队列,就是几分钟。
上代码:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct WoodenSticks{
 5     int l;
 6     int w;
 7 }w[5001];
 8 
 9 int main()
10 {
11     int f[5001];
12     int T;
13     cin>>T;
14     while(T--){
15         int n,i,j;
16         cin>>n;
17         //输入
18         for(i=1;i<=n;i++)
19             cin>>w[i].l>>w[i].w;
20         //从小到大排序。l相同就对w排序
21         for(i=1;i<n;i++)
22             for(j=1;j<=n-1;j++){
23                 if(w[j].l>w[j+1].l){      //当前木材的长度大于下一个木材的长度的话,调换位置
24                     int t1,t2;
25                     t1=w[j].l;t2=w[j].w;
26                     w[j].l=w[j+1].l;w[j].w=w[j+1].w;
27                     w[j+1].l=t1;w[j+1].w=t2;
28                 }
29                 else if(w[j].l==w[j+1].l)   //当前木材的长度等于下一个木材的长度的话,比较木材的重量,如果当前大于下一个的,调换位置。
30                     if(w[j].w>w[j+1].w){
31                         int t1,t2;
32                         t1=w[j].l;t2=w[j].w;
33                         w[j].l=w[j+1].l;w[j].w=w[j+1].w;
34                         w[j+1].l=t1;w[j+1].w=t2;
35                     }
36             }
37 
38 
39         int min=1;
40         f[1]=w[1].w;
41         for(i=2;i<=n;i++){
42             for(j=1;j<=min;j++){
43                 if(w[i].w>=f[j]){
44                     f[j]=w[i].w;     //存储当前各队列最大的数
45                     break;
46                 }
47             }
48             if(j>min){  //如果小于前面每一个数,则重新开一个队列,min+1
49                 min++;
50                 f[min]=w[i].w;
51             }
52         }
53         cout<<min<<endl;
54     }
55     return 0;
56 }

 

posted @ 2013-05-19 21:24  Freecode#  阅读(218)  评论(0编辑  收藏  举报