杭电 1051 Wooden Sticks

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 }
时间: 2024-10-09 18:58:25

杭电 1051 Wooden Sticks的相关文章

HDOJ 1051. Wooden Sticks

HDOJ 1051. Wooden Sticks 题目 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 ma

Wooden Sticks(杭电1051)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12157    Accepted Submission(s): 5036 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

1051 Wooden Sticks(贪心-3)

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

HDU 1051 Wooden Sticks【贪心+排序】

/* 中文题目 木头 中文翻译-大意 加工第一个木头要调试机器,所以要一分钟,后面的木头在长度和重量上都要大于等于前一个木头,才不要调试机器,否则要调试机器,再花费一分钟 解题思路:先将木头的长度按升序排序,后面质量发生冲突的地方就要在之后重新安排一下 解题人:lingnichong 解题时间:2014-10-25 16:33:10 解题体会:一个括号的丢失,就导致错了一大片 */ Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    M

HDU 1051 - Wooden Sticks

贪心吧 保证一维非递减的情况下,计算另一维上最少有几个非递减序列,就是答案 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 int t,n; 6 struct P{ 7 int l,w; 8 }s[5005]; 9 bool cmp(P a,P b) 10 { 11 return a.l==b.l?a.w<b.w : a.l<b

贪心/hdu 1051 Wooden Sticks

#include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct node { int l,w; bool v; }; int n; node a[5010]; bool cmp(node x,node y) { if (x.l==y.l) return x.w<y.w; return x.l<y.l; } int main() { int T; scanf(