首页 > 其他 > 详细

Sunscreen POJ 3614(贪心)

时间:2019-08-27 13:38:53      阅读:90      评论:0      收藏:0      [点我收藏+]

原题

题目链接

题目分析

贪心题,想一下就会发现对于一种防晒乳,应该优先给那些最大SPF等级较低的牛用.因为他们能用的防晒乳更少.实现的话只需要把防晒乳的SPF等级按小到大排序,然后没遍历一种防晒乳就把能用这瓶防晒乳的牛全部加入一个优先队列中,优先队列取出最大SPF等级最小的牛即可.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set>
13 
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18 
19 typedef pair<int,int> P;
20 P cow[2500];
21 P lotion[2500];
22 
23 int main()
24 {
25 //    freopen("black.in","r",stdin);
26 //    freopen("black.out","w",stdout);
27     int c,l;
28     cin>>c>>l;
29     for(int i=0;i<c;i++) cin>>cow[i].first>>cow[i].second;
30     for(int i=0;i<l;i++) cin>>lotion[i].first>>lotion[i].second;
31     sort(cow,cow+c);
32     sort(lotion,lotion+l);
33     int ans=0,cnt=0;
34     priority_queue<int,vector<int>,greater<int> > que;
35     for(int i=0;i<l;i++)
36     {
37         while(cow[cnt].first<=lotion[i].first&&cnt<c) que.push(cow[cnt].second),cnt++;
38         while(que.size())
39         {
40             if(!lotion[i].second) break;
41             int p=que.top();que.pop();
42   //          printf("%d num=%d lotion %d p %d\n",i,lotion[i].second,lotion[i].first,p);
43             if(lotion[i].first<=p) lotion[i].second--,ans++;
44 
45         }
46     }
47     cout<<ans<<endl;
48     return 0;
49 }

 

Sunscreen POJ 3614(贪心)

原文:https://www.cnblogs.com/VBEL/p/11417744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!