贪心入门题解

A-hdu2037

题意:给定若干电视节目的开始和结束时间(整点时刻),试计算出最多能完整看完的节目数。

思路:按照节目结束时间排序,选择结束时间靠前的节目。  

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node
31 {
32     int s,e;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     return a.e<b.e;
38 }
39 
40 int main()
41 {
42     int n;
43     while(~scanf("%d",&n),n)
44     {
45         for(int i=1;i<=n;++i)    scanf("%d %d",&p[i].s,&p[i].e);
46         sort(p+1,p+1+n,cmp);
47         int ans=0,tmp=-1;
48         for(int i=1;i<=n;++i)
49         {
50             if(p[i].s>=tmp)    ans++,tmp=p[i].e;
51         }
52         printf("%d\n",ans);
53     }
54 }
代码

B-hdu1009

题意:已知若干烧烤店的肉的数量和总价,问给定金额的钱最多可以购买多少肉。

思路:计算给定肉的单价,按照单价排序,购买性价比高的。

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node
31 {
32     double x,y,a;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     return a.a>b.a;
38 }
39 
40 int main()
41 {
42     int n,m;
43     while(~scanf("%d %d",&m,&n))
44     {
45         if(n==-1&&m==-1)    break;
46         for(int i=1;i<=n;++i)
47         {
48             scanf("%lf %lf",&p[i].x,&p[i].y);
49             p[i].a=p[i].x/p[i].y;
50         }
51         sort(p+1,p+1+n,cmp);
52         double ans=0;
53         for(int i=1;i<=n;++i)
54         {
55             if(m>=p[i].y)    
56             {
57                 m-=p[i].y;
58                 ans+=p[i].x;
59             }
60             else 
61             {
62                 ans+=p[i].a*m;
63                 m=0;
64             }
65         }
66         printf("%.3f\n",ans);
67     }
68 }
代码

C-hdu1050

题意:给你400个房间,每个房间标号,一条栈道的一边全是奇数,另一边全是偶数,每十分钟内每个位置的两个房间中间的栈道只能通行1次,问把这n条桌子从不同房间放到另一个房间中至少要几分钟。

思路1:贪心。每次同时移动尽可能多的桌子,需要移动的次数$*10$。

思路2:统计每个位置最多的通行次数,即至少移动的次数。

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node
31 {
32     int l,r,flag;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     return a.l<b.l;
38 }
39 
40 int main()
41 {
42     int t;
43     scanf("%d",&t);
44     while(t--)
45     {
46         int n;
47         scanf("%d",&n);
48         for(int i=1;i<=n;++i)
49         {
50             scanf("%d %d",&p[i].l,&p[i].r);
51             p[i].flag=0;
52             p[i].l=(p[i].l+1)/2;
53             p[i].r=(p[i].r+1)/2;
54             if(p[i].l>p[i].r)    swap(p[i].l,p[i].r);
55         }
56         sort(p+1,p+1+n,cmp);
57         int R=-1,ans=0;
58         for(int i=1;i<=n;++i)
59         {
60             if(p[i].flag)    continue;
61             R=p[i].r;
62             p[i].flag=1;
63             ans++;
64             for(int j=i+1;j<=n;++j)
65             {
66                 if(p[j].flag)    continue;
67                 if(p[j].l>R)    
68                 {
69                     R=p[j].r;
70                     p[j].flag=1;
71                 }
72             }
73         }
74         printf("%d\n",ans*10);
75     }
76 }
思路1代码
 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 int cnt[maxn];
31 
32 int main()
33 {
34     int t;
35     scanf("%d",&t);
36     while(t--)
37     {
38         int n,l,r;
39         scanf("%d",&n);
40         int ans=0;
41         memset(cnt,0,sizeof cnt);
42         for(int i=1;i<=n;++i)
43         {
44             scanf("%d %d",&l,&r);
45             l=(l+1)/2;
46             r=(r+1)/2;
47             if(l>r) swap(l,r);
48             for(int j=l;j<=r;++j) cnt[j]++;
49         }
50         for(int i=0;i<=200;++i)    ans=max(ans,cnt[i]);
51         printf("%d\n",ans*10);
52     }
53 }
思路2代码

D-poj1017

题意:一家工厂生产的产品规格分为1×1, 2×2, 3×3, 4×4, 5×5, 6×6,高都是h。工厂要把它们包在6×6×h的包装袋中。工厂想让包装数尽可能少。

思路:优先装规格大的产品。可以试着画图,比如装$3*3$规格的产品的时候,四个四个装,如果有多余的部分分情况讨论。注意细节问题。

 1 //#include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 int a[10];
31 
32 int main()
33 {
34     while(~scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
35     {
36         for(int i=1;i<=6;++i)    
37         {
38             if(a[i])    break;
39             if(i==6)    return 0;    
40         }
41         int ans=a[6];
42         if(a[5])
43         {
44             ans+=a[5];
45             a[1]=max(0,a[1]-a[5]*11);
46         }
47         if(a[4])
48         {
49             ans+=a[4];
50             if(a[4]*5>a[2])
51             {
52                 a[1]=max(0,a[1]-(a[4]*5-a[2])*4);
53                 a[2]=0;
54             }
55             else a[2]-=a[4]*5;
56         }
57         if(a[3])
58         {
59             ans+=a[3]/4;
60             a[3]%=4;
61             if(a[3]==1 )
62             {
63                 ans++;
64                 if(a[2]>=5)    a[2]-=5,a[1]=max(0,a[1]-7);
65                 else a[1]=max(0,a[1]-7-(5-a[2])*4),a[2]=0;
66             }
67             else if(a[3]==2)
68             {
69                 ans++;
70                 if(a[2]>=3)    a[2]-=3,a[1]=max(0,a[1]-6);
71                 else a[1]=max(0,a[1]-6-(3-a[2])*4),a[2]=0;
72             }
73             else if(a[3]==3)
74             {
75                 ans++;
76                 if(a[2]>=1)    a[2]-=1,a[1]=max(0,a[1]-5);
77                 else a[1]=max(0,a[1]-9);
78             }
79         }
80         if(a[2])
81         {
82             ans+=a[2]/9;
83             a[2]%=9;
84             if(a[2]) ans++;
85             a[1]=max(0,a[1]-(6*6-a[2]*4));
86         }
87         if(a[1])
88         {
89             ans+=a[1]/36;
90             a[1]%=36;
91             if(a[1])    ans++;
92         }
93         printf("%d\n",ans);
94     }
95 }
代码

E-hdu1051

题意:将木棍放在机器里处理,第一根需要一分钟,剩余的如果大于等于前边放入的长度和重量,就不用费时间,否则需要一分钟,求处理完这堆木棍的最短时间。

思路:先按照木棍长度排序,长度一样按照重量排序。因长度有序,只需考虑重量,对于当前的每根木棍,只需判断重量是否大于上一根即可。

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node 
31 {
32     int l,w,flag;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     if(a.l==b.l)    return a.w<b.w;
38     return a.l<b.l;
39 }
40 
41 int main()
42 {
43     int t;
44     scanf("%d",&t);
45     while(t--)
46     {
47         int n;
48         scanf("%d",&n);
49         for(int i=1;i<=n;++i)
50         {
51             scanf("%d %d",&p[i].l,&p[i].w);
52             p[i].flag=0;
53         }
54         sort(p+1,p+1+n,cmp);
55         int ans=0,tmp;
56         for(int i=1;i<=n;++i)
57         {
58             if(p[i].flag)    continue;
59             ans++;
60             p[i].flag=1;
61             tmp=p[i].w;
62             for(int j=i+1;j<=n;++j)
63             {
64                 if(p[j].flag)    continue;
65                 if(p[j].w>=tmp)
66                 {
67                     p[j].flag=1;
68                     tmp=p[j].w;
69                 }
70             }
71         }
72         printf("%d\n",ans);
73     }
74 }
代码

F-hdu1338

题意:假定包括你一共有$M$个人在玩一个特殊的卡片游戏,一开始,每个玩家拿$N$张卡片,卡的点数最多是$N*M$。而且卡数是互异的,每一轮中,每个玩家选择一张卡和其它玩家比较,谁的点数最大谁就赢得这一轮.然后开始下一轮,在$N$轮之后,当所有玩家的卡片都选过之后,赢得轮数最多的玩家获胜.给你一开始的卡片,说出你至少赢得多少轮。

思路:问的至少所以要考虑最坏的情况。以为只要对手中有一个人的牌更大,那么就是输。对面的玩家根据手里的牌联合可以算出我的牌,假设对方知道我的出牌顺序,那么这是最坏的情况。假设我是从大到小出牌,如果对面有比我大的牌,只需要出一张最大的牌以及$M-2$张最小的牌,这是在保证对手赢的最优出牌方式。如果对面发现没有比我还要大的牌,那么这局我必赢,所以对面会全部出最小的牌。

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 int a[maxn];
31 int vis[maxn];
32 queue<int>q;
33 
34 bool cmp(const int &a,const int &b)
35 {
36     return a>b;
37 }
38 
39 int main()
40 {
41     int n,m,cas=0;
42     while(~scanf("%d %d",&m,&n),n,m)
43     {
44         while(!q.empty())    q.pop();
45         memset(vis,0,sizeof vis);
46         for(int i=1;i<=n;++i) scanf("%d",&a[i]),vis[a[i]]=1;
47         sort(a+1,a+1+n,cmp);
48         for(int i=n*m;i>=1;--i)
49         {
50             if(vis[i]) continue ;
51             q.push(i);
52         }
53         int ans=0;
54         for(int i=1;i<=n;++i)
55         {
56             int x=q.front();
57             if(a[i]>x) ans++;
58             else q.pop();
59         }
60         printf("Case %d: %d\n",++cas,ans);
61     }
62 }
代码

G-hdu1789

题意:有$n$门功课,每门功课有截止时间和未完成需要扣除的分数,没完成一门功课需要花费一天的时间,问最少需要扣除的分数。

思路:因为没门作业需要完成的时间是一样的,所以我们优先做需要扣除分数多的功课,并且在可以完成的时间内越迟完成越好。

 1 #include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node
31 {
32     int val,time;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     return a.val>b.val;
38 }
39 
40 int vis[maxn];
41 
42 int main()
43 {
44     int t;
45     scanf("%d",&t);
46     while(t--)
47     {
48         int n;
49         scanf("%d",&n);
50         memset(vis,0,sizeof(vis));
51         for(int i=1;i<=n;++i)    scanf("%d",&p[i].time);
52         for(int i=1;i<=n;++i)    scanf("%d",&p[i].val);
53         sort(p+1,p+1+n,cmp);
54         int ans=0;
55         for(int i=1;i<=n;++i)
56         {
57             ans+=p[i].val;
58             for(int j=p[i].time;j>=1;--j)
59             {
60                 if(vis[j])    continue;
61                 vis[j]=1;
62                 ans-=p[i].val;
63                 break;
64             }
65         }
66         printf("%d\n",ans);
67     }
68 }
代码

H-poj1700

题意:只有一艘船,能乘$2$人,船的运行速度为$2$人中较慢一人的速度,过去后还需一个人把船划回来,问把$n$个人运到对岸,最少需要多久。

思路:首先按照速度排序。 考虑单独把最慢的两个人运到河对岸的最快方案。

    1.最快的和次快的过河,然后最快的将船划回来;次慢的和最慢的过河,然后次快的将船划回来。

       2.最快的和最慢的过河,然后最快的将船划回来,最快的和次慢的过河,然后最快的将船划回来。

    两种方案需要的时间分别为: 1. $t[2] + t[1] + t[n] + t[2] = t[1] + 2 * t[2] + t[n]$

                  2. $t[n] + t[1] + t[n-1]+t[1] = 2 * t[1] +t[n-1] + t[n]$

 1 //#include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 int a[maxn];
31 
32 int main()
33 {
34     int t;
35     scanf("%d",&t);
36     while(t--)
37     {
38         int n;
39         scanf("%d",&n);
40         for(int i=1;i<=n;++i)
41         {
42             scanf("%d",&a[i]);
43         }
44         sort(a+1,a+1+n);
45         int ans=0;
46         while(n>=4)
47         {
48             ans+=min(a[1]*2+a[n-1]+a[n],a[1]+a[2]*2+a[n]);
49             n-=2;
50         }
51         if(n==3) ans+=a[3]+a[1]+a[2];
52         if(n==2) ans+=a[2];
53         if(n==1) ans+=a[1];
54         printf("%d\n",ans);
55     }
56 }
代码

I-poj1328

题意:一个二维坐标系的一二象限代表海,三四象限代表陆地,$x$轴代表海岸线,海里有一些岛屿,海岸线上准备装一些雷达,每个雷达的作用范围是一个半径为$d$的圆,问覆盖全部岛屿最少需要装多少个雷达。

思路:根据岛屿的位置,我们可以计算出海岸线上需要装雷达的范围。可以用 $[ l , r ]$ 区间的线段表示。 那么我们的问题就转换为至少需要多少个点可以覆盖这些线段。 把线段先按照 $l$ 排序,再按照 $r$ 排序。 判断线段的关系:如果两个区间相交,无需操作;如果一个区间完全包含于另外一个区间,我们需要更新区间的右端点;如果两个区间不相交,我们需要增加点并更新右端点。

 1 //#include<bits/stdc++.h>
 2 #include <set>
 3 #include <map>
 4 #include <stack>
 5 #include <cmath>
 6 #include <queue>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <iostream>
12 #include <algorithm>
13 
14 #define ll long long
15 #define PLL pair<ll,ll>
16 #define PII pair<int,int>
17 #define bug printf("*********\n")
18 #define FIN freopen("input.txt","r",stdin);
19 #define FON freopen("output.txt","w+",stdout);
20 #define IO ios::sync_with_stdio(false),cin.tie(0)
21 #define ls root<<1
22 #define rs root<<1|1
23 
24 using namespace std;
25 const int inf=0x3f3f3f3f;
26 const ll Inf=1e14+7;
27 const int maxn=1e5+5;
28 const int mod=1e9+7;
29 
30 struct node
31 {
32     double l,r;
33 }p[maxn];
34 
35 bool cmp(const node &a,const node &b)
36 {
37     if(a.l==b.l)    return a.r<b.r;
38     return a.l<b.l;
39 }
40 
41 int main()
42 {
43     int n,d;
44     int cas=0;
45     while(~scanf("%d %d",&n,&d),n,d)
46     {
47         int flag=0;
48         for(int i=1;i<=n;++i)
49         {
50             int x,y;
51             scanf("%d %d",&x,&y);
52             if(y>d)    flag=1;
53             else
54             {
55                 double s=sqrt(d*d-y*y);
56                 p[i].l=x-s;
57                 p[i].r=x+s;
58             }
59         }
60         if(flag)    printf("Case %d: -1\n",++cas);
61         else 
62         {
63             sort(p+1,p+1+n,cmp);
64             double R=p[1].r;
65             int ans=1;
66             for(int i=2;i<=n;++i)
67             {
68                 if(p[i].l<=R && p[i].r>=R)    continue;
69                 else if(p[i].l<=R && p[i].r<R) R=p[i].r;
70                 else ans++,R=p[i].r;
71             }
72             printf("Case %d: %d\n",++cas,ans);
73         }
74     }
75 }
代码

猜你喜欢

转载自www.cnblogs.com/zhang-Kelly/p/12283942.html
今日推荐