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
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[n].d+1<day)
break;
}
printf("%d\n",ans);
return 0;
}

Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)的更多相关文章

  1. Codeforces Round #252 (Div. 2) B. Valera and Fruits

    #include <iostream> #include <vector> #include <algorithm> #include <map> us ...

  2. Codeforces Round #252 (Div. 2) 441B. Valera and Fruits

    英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...

  3. codeforces Round #252 (Div. 2) C - Valera and Tubes

    贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #i ...

  4. Codeforces Round #252 (Div. 2) A - Valera and Antique Items

    水题 #include <iostream> #include <set> #include <vector> #include <algorithm> ...

  5. Codeforces Round 252 (Div. 2)

    layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  6. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  7. [Codeforces Round #237 (Div. 2)] A. Valera and X

    A. Valera and X time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  8. Codeforces Round #216 (Div. 2) D. Valera and Fools

    题目链接:http://codeforces.com/contest/369/problem/D 注意题意:所有fools都向编号最小的fool开枪:但每个fool都不会笨到想自己开枪,所以编号最小的 ...

  9. Codeforces Round #252 (Div. 2) D

    http://codeforces.com/problemset/problem/441/D 置换群的基本问题,一个轮换内交换成正常顺序需要k-1次,k为轮换内元素个数 两个轮换之间交换元素,可以把两 ...

随机推荐

  1. ubuntu 安装Opencv2.4.7

    1.安装Cmake: sudo apt-get install cmake 2.到Opencv目录下创建文件夹release $ cmake -D CMAKE_BUILD_TYPE=RELEASE - ...

  2. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  3. boost 循环缓冲区

    boost 循环缓冲区 #include <boost/circular_buffer.hpp> int _tmain(int argc, _TCHAR* argv[]) { boost: ...

  4. 4Sum -- LeetCode

    原题链接: http://oj.leetcode.com/problems/4sum/  这道题要求跟3Sum差点儿相同,仅仅是需求扩展到四个的数字的和了.我们还是能够依照3Sum中的解法,仅仅是在外 ...

  5. Spring通过工厂创建实例的注意事项

    假设第三方(or别的team)提供一个工厂类(此类是不能够改动的.往往以jar包形式提供的),须要供给我们项目来使用. 可是我们自己的项目使用了spring来配置,所以我们当然希望可以通过spring ...

  6. UVA 10160 Servicing Stations(深搜 + 剪枝)

    Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...

  7. HDU 3068 最长回文 Manacher算法

    Manacher算法是个解决Palindrome问题的O(n)算法,能够说是个超级算法了,秒杀其它一切Palindrome解决方式,包含复杂的后缀数组. 网上非常多解释,最好的解析文章当然是Leetc ...

  8. EasyUI - Progressbar 进度条控件

    效果: html代码: <div id="p" style="width:400px;"></div> JS代码: $(function ...

  9. 用DELPHI的RTTI实现对象的XML持久化

    去年我花了很多时间尝试用DELPHI进行基于XML的WEB应用开发.起初的设想是很美好的,但结果做出来的东西很简陋.一部分原因就在于XML到Object之间的数据绑定实现太麻烦(另一部分是因为对XSL ...

  10. uva 1025

    紫皮书 非原创…… 某城市的地铁是线性的有n个车站从左到右编号为1-n,有M1辆地铁从第一站出发,有M2辆车从最后一站出发,mario从第一站出发,目的是在时刻T会见车站n的一个朋友(间谍).在车站等 ...