Codeforces 441 B. Valera and Fruits
1 second
256 megabytes
standard input
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?
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.
Print a single integer — the maximum number of fruit that Valera can collect.
2 3
1 5
2 3
8
5 10
3 20
2 20
1 20
4 20
5 20
60
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.
import java.util.*; public class Main
{
public static void main(String[] args)
{
class Tree
{
int a,b;
Tree(){}
Tree(int A,int B)
{
a=A; b=B;
}
}
Scanner cin=new Scanner(System.in);
int n,v;
n=cin.nextInt();
v=cin.nextInt();
Tree[] tree=new Tree[n];
for(int i=0;i<n;i++)
{
int a=cin.nextInt();
int b=cin.nextInt();
tree[i]=new Tree(a,b);
}
int[] havday=new int[3010];
for(int i=0;i<n;i++)
{
havday[tree[i].a]+=tree[i].b;
}
int fit=0,unfit=0,sum=0;
for(int i=1;i<=3001;i++)
{
fit=havday[i];
if(unfit>=v)
{
sum+=v;
unfit=fit;
}
else
{
sum+=unfit;
int carry=v-unfit;
if(carry>=fit)
{
sum+=fit;
unfit=0;
}
else
{
sum+=carry;
unfit=fit-carry;
}
}
}
System.out.println(sum);
}
}
Codeforces 441 B. Valera and Fruits的更多相关文章
- CodeForces 441 A. Valera and Antique Items
纯粹练JAVA.... A. Valera and Antique Items time limit per test 1 second memory limit per test 256 megab ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Valera and Fruits
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- codeforces 441B. Valera and Fruits 解题报告
题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...
- Codeforces Round #252 (Div. 2) B. Valera and Fruits
#include <iostream> #include <vector> #include <algorithm> #include <map> us ...
- Codeforces #252 (Div. 2) B. Valera and Fruits
题目倒是不难,可是读起来非常恶心 依据题目的描写叙述不easy找到适合存储的方法 后来我就想不跟着出题人的思路走 我自己开一个数组c 令c[a[i]] = b[i] 则c[i] == [j] 代表第i ...
- Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...
- C - Valera and Fruits
Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...
随机推荐
- 华为nova 4取代刘海屏
尽管首发被三星“截胡”,但华为依然是第一批发布“打孔屏”新机的厂商.官方已经确认,将于12月17日在长沙发布华为nova 4,主打自拍极点全面屏. 继真机谍照.配置曝光之后,今日华为官方发布一则华为n ...
- WPF获得PNG图片外观Path数据
原文:WPF获得PNG图片外观Path数据 WPF开发界面的时候,用的最多的就是自定义控件模板,开发人员需要根据UI的设计,做出符合要求的自定义控件.但是在一些特殊情况下,UI的设计可能 ...
- 【Henu ACM Round#20 F】 Arthur and Brackets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 所给的li,ri是左括号从左到右的顺序给的. (且注意长度是2*n 现在我们先把第一个左括号放在第1个位置. 然后考虑第二个位置. ...
- HIT 2255 Not Fibonacci(矩阵高速幂)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...
- Android页面事件挂接模拟
Java没有C#的引用类型.因此事件的挂接一般都是利用接口来实现,有两种方式: 1)定义一个实现事件接口的类,然后实现接口方法,然后通过将这个类的实例加入到事件监听器里面: public class ...
- caioj1442:第k小的数Ⅱ
[传送门:caioj1442] 简要题意: 给出n个点,每个点都有一个权值,m个操作,操作有两种:第一种是询问l到r的第k小的值,然后输出这个值,第二种是将第x个点的值改为k 题解: 又是一道主席树的 ...
- Import .bak file to a database in SQL server
https://stackoverflow.com/questions/1535914/import-bak-file-to-a-database-in-sql-server On SQL Serve ...
- Android View体系(十)自定义组合控件
相关文章 Android View体系(一)视图坐标系 Android View体系(二)实现View滑动的六种方法 Android View体系(三)属性动画 Android View体系(四)从源 ...
- 10.ng-class-even与ng-class-odd
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS模板使你可以把该作用域内的数据直接绑定到所显示的HTML元素 ng-class-even与n ...
- Js怎么获取DOM及获取浏览器的宽高?
在JavaScript中,经常会需要获取document文档元素,是HTML文档对象模型的缩写,HTML DOM 定义了用于 HTML 的一系列标准的对象,以及访问和处理 HTML 文档的标准方法. ...