Codeforces Round #252 (Div. 2) B. Valera and Fruits
2014-07-30 11:17
447 查看
Let's start counting days from 1 to 3001. Let current day be i. Additionally, we'll have cur variable
— number of fruit we didn't collect previous days. Suppose now fruit is ripen current day. If now + cur ≤ v,
we need to add now + cur to answer and update cur value
(cur = 0). Otherwise we add v to answer, but cur value
need to be updated as follows. Let tv = max(v - cur, 0). Then cur = now - tv.
In other words, we try to collect fruits that will not be collectable next day.
Additionally, problem could be solved with
![](http://espresso.codeforces.com/06b2dfff448a3a9e0e9b5c879f5cda0ce1cecba5.png)
;
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int a,b;
bool operator <(const Node &t)const{
return a<t.a;
}
};
Node a[3005];
int n,v;
void solve() {
int fromLastDays = 0;
int ans = 0;
for(int day = 1; day <= 3001; day++) {
int curDay = 0;
for(int i=0;i<n;i++)
if (a[i].a == day)
curDay += a[i].b;
if (curDay + fromLastDays <= v) {
ans += fromLastDays + curDay;
fromLastDays = 0;
} else {
ans += v;
int tv = v - fromLastDays;
if (tv < 0) tv = 0; //注意这里当tv<0的时候要变成0
fromLastDays = curDay - tv;
}
}
cout << ans << endl;
}
int main(){
int i,j;
while(scanf("%d%d",&n,&v)!=EOF){
memset(a,0,sizeof(a));
for(i=0;i<n;i++){
scanf("%d %d",&a[i].a,&a[i].b);
}
solve();
}
return 0;
}
— number of fruit we didn't collect previous days. Suppose now fruit is ripen current day. If now + cur ≤ v,
we need to add now + cur to answer and update cur value
(cur = 0). Otherwise we add v to answer, but cur value
need to be updated as follows. Let tv = max(v - cur, 0). Then cur = now - tv.
In other words, we try to collect fruits that will not be collectable next day.
Additionally, problem could be solved with
![](http://espresso.codeforces.com/06b2dfff448a3a9e0e9b5c879f5cda0ce1cecba5.png)
;
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int a,b;
bool operator <(const Node &t)const{
return a<t.a;
}
};
Node a[3005];
int n,v;
void solve() {
int fromLastDays = 0;
int ans = 0;
for(int day = 1; day <= 3001; day++) {
int curDay = 0;
for(int i=0;i<n;i++)
if (a[i].a == day)
curDay += a[i].b;
if (curDay + fromLastDays <= v) {
ans += fromLastDays + curDay;
fromLastDays = 0;
} else {
ans += v;
int tv = v - fromLastDays;
if (tv < 0) tv = 0; //注意这里当tv<0的时候要变成0
fromLastDays = curDay - tv;
}
}
cout << ans << endl;
}
int main(){
int i,j;
while(scanf("%d%d",&n,&v)!=EOF){
memset(a,0,sizeof(a));
for(i=0;i<n;i++){
scanf("%d %d",&a[i].a,&a[i].b);
}
solve();
}
return 0;
}
相关文章推荐
- Codeforces Round #252(Div. 2) 441B. Valera and Fruits 贪心
- Codeforces Round #252(Div. 2) 441C. Valera and Tubes 构造
- Codeforces Round #252(Div. 2) 441D. Valera and Swaps 置换群
- codeforces Round #252 (Div. 2) C - Valera and Tubes
- Codeforces Round #237 (Div. 2) A. Valera and X(模拟题)
- Codeforces Round #252 (Div. 2) C. Valera and Tubes
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 镜面对称 博弈论
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
- Codeforces Round #207 (Div. 2) Xenia and Hamming(字符串匹配)
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪心、数学)
- Codeforces Round #379 (Div. 2) F. Anton and School
- Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串
- Codeforces Round #428 (Div. 2) A. Arya and Bran
- Codeforces Round #305 (Div. 2)——D单调/路径压缩——Mike and Feet
- Codeforces Round #305 (Div. 2), problem: (A) Mike and Fax
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
- Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)C - Qualification
- Codeforces Round #291 (Div. 2)-D. R2D2 and Droid Army(RMQ)
- Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
- Codeforces Round #316 (Div. 2) E. Pig and Palindromes