码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 1051 Wooden Sticks

时间:2016-08-11 17:35:18      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

大意:
  

描述

C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于

 

第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?

 

输入

第一行是一个整数T,表示输入数据一共有T组。

每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示

 

木棒的长度和质量。

 

输出

处理这些木棒的最短时间。

 

先将木头按长度从小到大排列,然后假设从第一个木头开始,查询第2~n个木头,把不用时间的全部标记,再找到下一个未标记的木头为开始,时间加一,再将这块木头后面不用时间的全部标记,以此类推。

 

 

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 struct stu
 5 {
 6     int l,w;
 7 }st[5010];
 8 bool cmp(stu a,stu b)
 9 {
10     if(a.l != b.l)                //将木头按长度从小到大排列,长度一样按质量从小到大排列 
11         return a.l<b.l;
12     else
13         return a.w<b.w;
14 }
15 
16 int main()
17 {
18     int t;
19     scanf("%d",&t);
20     while(t--)
21     {
22         int i,j,d[5010];
23         int n;
24         scanf("%d",&n);
25         for(i = 0 ; i < n ; i++)
26         {
27             scanf("%d %d",&st[i].l,&st[i].w);
28             d[i]=0;                                //标记查询过的木头,0为未标记 
29         }
30         int ans=0;
31         sort(st,st+n,cmp);
32         for(i = 0 ; i < n ; i++)
33         {
34             if(!d[i])
35             {
36                 d[i]=1;
37                 ans++;                            //ans表示时间 
38                 int w=st[i].w;
39                 for(j = i+1 ; j < n ; j++)        //查询i以后的木头 
40                 {
41                     if(!d[j] && st[j].w >= w)    //因为木头的长度是从小到大排列,所以这只判断重量 
42                     {
43                         d[j]=1;                    //标记在i后处理不用时间的木头 
44                         w=st[j].w;
45                     }
46             
47                 }
48             }
49         }
50         printf("%d\n",ans);
51     }
52 }

 

 

 

杭电 1051 Wooden Sticks

标签:

原文地址:http://www.cnblogs.com/yexiaozi/p/5761749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!