Valera and Fruits CodeForces - 441B

原题链接

考察:贪心+模拟

思路:

        枚举从开始时间到结束时间每一天的收益.设收益为w,每天最多取min(树的苹果,v-w)个.注意不是最多取v个.因为是按天数枚举的.考虑存在苹果树没有取完隔天再取的情况,所以用了优先队列.

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 const int N = 3005;
 7 typedef pair<int,int> PII;
 8 int n,v;
 9 PII p[N];
10 struct cmp{
11     bool operator()(PII x,PII y){
12         if(x.first==y.first) return x.second<y.second;
13         return x.first>y.first;
14     }
15 };
16 priority_queue<PII,vector<PII>,cmp> q;
17 int solve()
18 {
19     int st = p[n].first,ed = p[1].first,res =0;
20     for(int i=st;i<=ed+1;i++)
21     {
22         int w = 0;
23         while(q.size()&&q.top().first<=i)
24         {
25             PII u = q.top(); q.pop();
26             if(u.first+1<i) continue;
27             int val =min(u.second,v-w);
28             w+=val; u.second-=val;
29             if(u.second) q.push(u);
30             if(w==v) break;
31         }
32         res+=w;
33     }
34     return res;
35 }
36 int main()
37 {
38     scanf("%d%d",&n,&v);
39     for(int i=1;i<=n;i++)
40       scanf("%d%d",&p[i].first,&p[i].second),q.push(p[i]);
41     sort(p+1,p+n+1,cmp());
42     int res = solve();
43     printf("%d\n",res);
44     return 0;
45 }

 

posted @ 2021-04-15 01:37  acmloser  阅读(52)  评论(0编辑  收藏  举报