HDU 1051(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051

Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13534    Accepted Submission(s):
5590

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

题目大意:一堆木棍,有长度和重量,处理第一根需要一分钟,然后处理下一根木棍,如果这根木棍的长度和重量都不大于之前的木棍,则不需要花费时间,否则也需要一分钟。

分析:首先排序,先按长度排序,如果长度一样,则按重量排序(从小到大和从大到小排序都可以,我是按从小到大排序的)。然后求递增(减)序列的最小个数。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 int T,n;
 9 int a[5005],b[5005],vis[5001];
10
11 int main ()
12 {
13     int i,j;
14     int time;
15     int x,y;
16     scanf ("%d",&T);
17     while (T--)
18     {
19         scanf ("%d",&n);
20         for (i=0; i<n; i++)
21             scanf ("%d %d",&a[i], &b[i]);
22         for (i=0; i<n; i++)//两个for循环排序,没用结构体,比较麻烦
23         {
24             for (j=i; j<n; j++)
25             {
26                 if (a[i] > a[j])
27                 {
28                     swap(a[i], a[j]);
29                     swap(b[i], b[j]);
30                 }
31                 if (a[i] == a[j])
32                 {
33                     if (b[i] > b[j])
34                     {
35                         swap(a[i], a[j]);
36                         swap(b[i], b[j]);
37                     }
38                 }
39
40             }
41         }
42         time = 0;
43         memset(vis, 0, sizeof(vis));
44         for (i=0; i<n; i++)
45         {
46             if (vis[i])//若该木棍被访问过,跳过
47                 continue;
48             //x = a[i];
49             y = b[i];
50             for (j=i+1; j<n; j++)
51             {
52                 if (!vis[j] && b[j] >= y)
53                 {
54
55                     vis[j] = 1;//标记访问
56                     //x = a[j];
57                     y = b[j];//当前最大重量
58                 }
59             }
60             time++;//时间加一
61         }
62         printf ("%d\n",time);
63     }
64     return 0;
65 }

时间: 2024-10-09 19:54:04

HDU 1051(贪心)的相关文章

hdu 1051 - 贪心,水题

题目链接 一堆小木棍,每个有两个属性值(l,w),对小木棍分组,每一组内的小木棍存在这样一个序列满足s1<=s2<=s3.....<=sn,[s1<=s2当且仅当s1.l<=s2.l&&s1.w<=s2.w],问最小的分组数 ------------------------------------------------------------------------------------------ 贪心 #include <set> #

HDU 1051 并查集+贪心

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 ar

HDU 1051:Wooden Sticks

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

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu