Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
2016-01-24 18:34
519 查看
B. Valera and Fruits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Valera loves his garden, where n fruit trees grow.
This year he will enjoy a great harvest! On the i-th tree bi fruit
grow, they will ripen on a day number ai.
Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and
day ai + 1 (all
fruits that are not collected in these two days, become unfit to eat).
Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more thanv fruits.
The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?
Input
The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) —
the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
Next n lines contain the description of trees in the garden. The i-th
line contains two space-separated integers ai and bi(1 ≤ ai, bi ≤ 3000) —
the day the fruits ripen on the i-th tree and the number of fruits on the i-th
tree.
Output
Print a single integer — the maximum number of fruit that Valera can collect.
Sample test(s)
input
output
input
output
Note
In the first sample, in order to obtain the optimal answer, you should act as follows.
On the first day collect 3 fruits from the 1-st
tree.
On the second day collect 1 fruit from the 2-nd
tree and 2 fruits from the 1-st
tree.
On the third day collect the remaining fruits from the 2-nd tree.
In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
题目链接:http://codeforces.com/contest/441/problem/B
题目大意:有n棵果树和每天最大的水果採摘量v。每棵果树有相应的採摘日期day,仅仅能在day和day+1两天採摘,求终于水果的最大採摘量。
解题思路:对每棵果树的採摘日期从小到大排序,日期靠前的先採摘。
代码例如以下:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Valera loves his garden, where n fruit trees grow.
This year he will enjoy a great harvest! On the i-th tree bi fruit
grow, they will ripen on a day number ai.
Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and
day ai + 1 (all
fruits that are not collected in these two days, become unfit to eat).
Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more thanv fruits.
The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?
Input
The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) —
the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
Next n lines contain the description of trees in the garden. The i-th
line contains two space-separated integers ai and bi(1 ≤ ai, bi ≤ 3000) —
the day the fruits ripen on the i-th tree and the number of fruits on the i-th
tree.
Output
Print a single integer — the maximum number of fruit that Valera can collect.
Sample test(s)
input
2 3 1 5 2 3
output
8
input
5 10 3 20 2 20 1 20 4 20 5 20
output
60
Note
In the first sample, in order to obtain the optimal answer, you should act as follows.
On the first day collect 3 fruits from the 1-st
tree.
On the second day collect 1 fruit from the 2-nd
tree and 2 fruits from the 1-st
tree.
On the third day collect the remaining fruits from the 2-nd tree.
In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
题目链接:http://codeforces.com/contest/441/problem/B
题目大意:有n棵果树和每天最大的水果採摘量v。每棵果树有相应的採摘日期day,仅仅能在day和day+1两天採摘,求终于水果的最大採摘量。
解题思路:对每棵果树的採摘日期从小到大排序,日期靠前的先採摘。
代码例如以下:
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const maxn=300005; struct A { int d,b; }a[3005]; int cmp(A p,A q) { return p.d<q.d; } int main() { int n,v,ans=0; scanf("%d%d",&n,&v); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].d,&a[i].b); } sort(a+1,a+n+1,cmp); for(int day=1;day<=maxn;day++) { int cur=0,p=0; //cur为每天的採摘量 for(int i=1;i<=n;i++) { if(a[i].d>day) //假设还未到达当前果树的日期,后面果树的日期也未到达,结束当前循环 break; if(a[i].d!=day&&a[i].d+1!=day) continue; if(a[i].b>0) //在规定的两天日期能够採摘 { p=1; if(a[i].b+cur<=v) //a[i].b表示当前果树上果实个数。假设採摘数量会有更新 { cur+=a[i].b; a[i].b=0; } else { a[i].b-=(v-cur); cur=v; break; } } } ans+=cur; if(a .d+1<day) break; } printf("%d\n",ans); return 0; }
相关文章推荐
- IOS8新特性之——UIAlertController的使用
- DotNet中StringBuilder和String的区别
- 两个collection联动(实现网易新闻首页效果)
- HDU 1047 多个大数相加 Integer Inquiry
- 【ElasticSearch】 QueryString查询中文歧义词异常问题解决
- 自定义UITextView
- 解决Xutils 的框架问题retry error, curr request is null
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
- this class is not key value coding-compliant for the key countryImageView-bug
- QuickLook(实战)
- HDU 1560 - DNA sequence
- androidUI控件
- 多年前写的文本框扩展控件(有ValueChanging事件等),已放github
- 不同Framework下StringBuilder和String的性能对比,及不同Framework性能比(附Demo)
- UIPickerView的自定义视图
- Sorting a Three-Valued Sequence
- hdu 1005 number sequence
- UVA 11995 I Can Guess The Data Structure!
- ZOJ 2812 Quicksum