hdu 贪心(hdu 2037,hdu 1051,hdu 1050)

hdu 2037   今年暑假不AC  (http://acm.hdu.edu.cn/showproblem.php?pid=2037

比较经典的贪心问题之一,以结束时间排序,如果下一个时间的开始时间大于等于上一个事件结束时间,那么结果就加一。

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     int start;
 9     int end;
10 }s[105];
11 bool cmp(node a,node b)
12 {
13     return a.end<b.end;
14 }
15 int main()
16 {
17      //freopen("input.txt","r",stdin);
18      //freopen("output.txt","w",stdout);
19     int n;
20     int i;
21     while(scanf("%d",&n)!=EOF)
22     {
23         if(n==0)  break;
24         for(i=0;i<n;i++)
25         {
26             scanf("%d%d",&s[i].start,&s[i].end);
27 
28         }
29         sort(s,s+n,cmp);
30 
31        // for(i=0;i<n;i++)
32            // printf("%d %d\n",s[i].start,s[i].end);
33 
34         int num=1;
35         int p=0;
36         for(int j=1;j<n;j++)
37         {
38             if(s[j].start>=s[p].end)
39             {
40                 num++;
41                 p=j;
42 
43             }
44 
45         }
46         printf("%d\n",num);
47 
48     }
49     return 0;
50 }

 

hdu 1051 Wooden Sticks(http://acm.hdu.edu.cn/showproblem.php?pid=1051

将木棍的长度和重量排序,然后设计一个二重循环一边遍历所有木棍一边更新,并在这个二重循环中统计需要的最少时间。

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 5003
 7 struct node
 8 {
 9     int length;
10     int weight;
11     int mark;
12 }s[N];
13 bool cmp(node a,node b)
14 {
15 
16     if(a.length!=b.length)
17         return a.length<b.length;
18     else
19         return a.weight<b.weight;
20 }
21 int main()
22 {
23    //freopen("input.txt","r",stdin);
24    // freopen("output.txt","w",stdout);
25     int t;
26     scanf("%d",&t);
27     while(t--)
28     {
29         int n;
30         scanf("%d",&n);
31         int i,j;
32         memset(s,0,sizeof(s));
33         for(i=0;i<n;i++)
34         {
35 
36             scanf("%d%d",&s[i].length,&s[i].weight);
37         }
38         sort(s,s+n,cmp);
39         int num=0;//统计时间
40         for(i=0;i<n;i++)//遍历每个木棍
41         {
42             if(s[i].mark==1)   continue;//如果该木棍考虑过了,进行下一个
43             num++;//时间数加一
44             int a=s[i].length,b=s[i].weight;
45             for(j=0;j<n;j++)//寻找长度和重量比第i个木棍大的木棍
46             {
47                 if(s[j].mark==1)  continue;
48                 if(s[j].length>=a&&s[j].weight>=b)
49                 {
50                     s[j].mark=1;
51                     a=s[j].length;b=s[j].weight;//这里一定要记得更新,刚开始忘记更新wrong了
52                 }
53 
54             }
55 
56 
57         }
58         printf("%d\n",num);
59     }
60     return 0;
61 
62 }

 

hdu 1050 Moving Tables(http://acm.hdu.edu.cn/showproblem.php?pid=1050

用一个数组num(范围为1-200)记录移动桌子所经过位置的次数,每次移动桌子经过位置i时,num[i]都加一,这样输出最大的num[i]即可。

 

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     int  t;
 9     scanf("%d",&t);
10     while(t--)
11     {
12         int n;
13         scanf("%d",&n);
14         int num[205];
15         memset(num,0,sizeof(num));
16         int i;
17         for(i=0;i<n;i++)
18         {
19             int a,b;
20             scanf("%d%d",&a,&b);
21             a=(a+1)/2;b=(b+1)/2;
22             if(a>b)
23                 swap(a,b);
24             int j;
25             for(j=a;j<=b;j++)
26                num[j]++;
27         }
28         int ans=0;
29         for(i=1;i<=200;i++)
30             if(ans<num[i])
31             ans=num[i];
32         printf("%d\n",ans*10);
33     }
34     return 0;
35 }

 

转载于:https://www.cnblogs.com/heat-man/archive/2013/05/03/3056021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值