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.
题目比較简单,但有两个easy忽略的地方,代码中已经标注了。
#include <iostream>
#include <cstdio>
using namespace std;
int t[3333];
int main()
{
int n,v,m,i,j,a,b;
cin>>n>>v;
int minn=9999,maxx=-1;
for(i=0;i<n;i++)
{
scanf("%d %d",&a,&b);
t[a]+=b;//可能多棵树在同一天成熟
maxx=max(maxx,a);
minn=min(minn,a);
}
int sum=0,tmp=0; for(i=minn;i<=maxx+1;i++)
{
if(tmp>=v)
{
tmp=t[i];
sum+=v;
}
else
{
if(tmp+t[i]<=v)
{ sum+=tmp+t[i];
tmp=0;//这里tmp位置非常重要,之前放在sum前面,就悲剧了
}
else
{
tmp=t[i]-(v-tmp);
sum+=v;
}
}
// cout<<tmp<<" "<<sum<<endl;
}
cout<<sum<<endl;
return 0;
}
Valera and Fruits的更多相关文章
- 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 ...
- Codeforces 441 B. Valera and Fruits
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...
- C - Valera and Fruits
Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...
- 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分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...
- Codeforces441B_Valera and Fruits(暴力)
Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round 252 (Div. 2)
layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...
随机推荐
- C语言调用正则表达式
标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一功能,其中最著名的当数Philip Hazel的Perl-Compatible Regular Expression库 ...
- spring_150908_hibernate_id_sequence
1.新建java工程:spring_150908_hibernate_id_sequence,添加相关jar包(spring.hibernate.ibatis)如下图所示: 2.实现实体类DogPet ...
- jquery on方法(事件委托)
jquery绑定事件处理函数的方法有好几个,比如:bind(),on(),delegate(),live(). 其中delegate和live都是用on实现的,效果也类似,live好像在1.7版本中已 ...
- 项目里用到的python知识点
1 ini文件处理创建ini文件config = configparser.ConfigParser()config.read(AUTH_STATUS_FILE)config.add_section( ...
- 长沙理工大学第十二届ACM大赛-重现赛 K - 大家一起来数二叉树吧
题目描述 某一天,Zzq正在上数据结构课.老师在讲台上面讲着二叉树,zzq在下面发着呆. 突然zzq想到一个问题:对于一个n个节点,m个叶子的二叉树,有多少种形态呐?你能告诉他吗? 对于第一组样例的解 ...
- Android之 广播
(以下内容是阅读郭霖大神的<第一行代码>后自己总结的) 1.概述 广播是Android的四大组件之一. Android的广播机制十分灵活. 2.发送广播 如上图Android的广播主要分为 ...
- TCP/IP——IP网络协议简记
IP提供不可靠.无连接的数据报传送服务 不可靠:不保证IP数据报能成功到达目的地,当发生错误时,IP的做法是丢弃这个数据报,然后发送ICMP消息报给信息源. 无连接:IP不维护任何关于后续数据报的状态 ...
- PHP 笔记——操作MySQL数据库
1. 连接MySQL服务器 mysqli_connect :此函数是该函数的别名: mysqli::__construct() mysqli mysqli_connect ( [string se ...
- BZOJ3052 [wc2013] 糖果公园 【树上莫队】
树上莫队和普通的序列莫队很像,我们把树进行dfs,然后存一个长度为2n的括号序列,就是一个点进去当作左括号,出来当作右括号,然后如果访问从u到v路径,我们可以转化成括号序列的区间,记录x进去的时候编号 ...
- Problem C: 深入浅出学算法004-求多个数的最小公倍数
Description 求n个整数的最小公倍数 Input 多组测试数据,先输入整数T表示组数 然后每行先输入1个整数n,后面输入n个整数k1 k2...kn Output 求k1 k2 ...kn的 ...