C - Valera and Fruits
Problem description
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 than v 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.
Examples
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.
解题思路:题目的意思就是有n棵树,a,b分别表示一棵树第a天成熟和该树树上有b个果实,并且只能在第a天或第a+1天这两天摘其树上的果实,超过这两天就不能再摘该树树上的果实(已发霉)。求农夫可以摘的最大果实数目。按照提示给的规则,刚开始做法是用结构体数组,但发现如果成熟的天数是断续的,这时候就会覆盖当前果树本该要摘的果实数目,从而少摘了一些果实。以下代码中的结构体数组按果树的成熟天数从小到大排完序后其连续下标存放的是成熟天数(成熟天数可能有断续即不连续)。举个栗子:n=4,v=10;(这里不举例有相同成熟天数的一些果树)
3 20
1 20
4 20
5 20
此样例正确输出应为60,但以下代码的输出为50(错误),因此不能这样子做。过程比较简单,可以自己模拟一下。
贴一下错误代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct NODE{
int dayth,num;
}node[maxn];
bool cmp(NODE x,NODE y){return x.num<y.num;}
int main(){
int n,v,x,y,tmp,sum=,k=;bool flag;
cin>>n>>v;
for(int i=;i<maxn;++i)node[i].num=;
for(int i=;i<=n;++i){
cin>>x>>y;flag=false;
for(int j=;j<k;++j)
if(node[i].dayth==x){node[i].num+=y;flag=true;break;}
if(!flag){node[k].dayth=x;node[k++].num=y;}
}
sort(node,node+k,cmp);
if(node[].num>=v){sum+=v;node[].num-=v;}
else {sum+=node[].num;node[].num=;}
for(int i=;i<=k;++i){
if(node[i-].num>=v)sum+=v;
else{
sum+=node[i-].num;tmp=v-node[i-].num;
if(node[i].num>=tmp){sum+=tmp;node[i].num-=tmp;}
else {sum+=node[i].num;node[i].num=;}
}
}
cout<<sum<<endl;
return ;
}
我们定义了一个数组r[i]=value,其下标i表示第i天成熟,value为果实数目。注意某些树成熟的天数可能是相同的,因而要将它们的果实累加。枚举每一天是否有成熟的果实可以摘,并且按规则摘取,这样就不会有覆盖,即发生少摘的情况。暴力贪心模拟即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int main(){
int n,v,x,y,tmp,sum=,r[maxn];//x,y分别表示某棵树成熟的天数和该树树上的果实数量
cin>>n>>v;
memset(r,,sizeof(r));//注意清0
for(int i=;i<=n;++i){cin>>x>>y;r[x]+=y;}//可能有一些树成熟的时间相同,所以要将其果实相加
for(int i=;i<maxn;++i){//从1开始枚举
if(r[i-]>=v)sum+=v;//如果上一棵树可以摘的果实数目不小于v,则直接摘掉v个,并且摘完后上一棵树已经过期了,此后不能再摘了
else{
sum+=r[i-];tmp=v-r[i-];//否则就直接摘掉上一棵树剩下的果实
if(r[i]>=tmp){sum+=tmp;r[i]-=tmp;}//如果当前这棵树可以摘的果实还大于一天剩下可摘的果实tmp,就摘掉tmp个
else{sum+=r[i];r[i]=;}//否则就直接摘掉当前这棵树上的果实
}
}
cout<<sum<<endl;
return ;
}
C - 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 ...
- 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 ...
- 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 ...
随机推荐
- 06--谈谈:C++类的“包含”机制
谈谈:C++类的“包含”机制 本人在学习Qt的时候发现了一个非常有趣的现象.有很多函数的调用方法都写成了如下的形式: object.func().func2(); 这令小弟着实不懂.在上面这段代码 ...
- Maven服务器的使用之Maven桌面项目和Maven Web项目的创建
Maven的使用 Maven功能强大, 可以参与管理软件的整个生命周期. Java软件开发中的jar包管理更是Maven的绝技. 1.创建Maven桌面项目 1.1 选择菜单创建Maven项目 1.2 ...
- PyCharm与GitHub环境配置
转载地址:https://blog.csdn.net/xierhacker/article/details/70053162 一.准备工作 Ⅰ.git下载和安装 要连接GitHub,首先git是必不可 ...
- javaee 用Buffered进行对象的写入和读取
package Zjshuchu; import java.io.Serializable; public class Dog implements Serializable{ private ...
- 连接mysql时遇到的问题
1.报错:The server time zone value '???ú±ê×??±??' is unrecognized or represents 解决方法:在jdbc连接的url后面加上ser ...
- PAT_A1134#Vertex Cover
Source: PAT A1134 Vertex Cover (25 分) Description: A vertex cover of a graph is a set of vertices su ...
- [基准测试]----lmbench
引言 要评价一个系统的性能,通常有不同的指标,相应的会有不同的测试方法和测试工具,一般来说为了确保测试结果的公平和权威性,会选用比较成熟的商业测试软件.但在特定情形下,只是想要简单比较不同系统或比较一 ...
- http://www.phplo.com/special/2013/0616/467.html
http://www.phplo.com/special/2013/0616/467.html
- Git 基础教程 之 解决合并冲突
① 解决冲突 把Git合并失败的文件手动编辑成我们希望的内容 ② 提交,合并完成 场景: ① 新分支feature1,修改后,add, commit: ② 切回master后,修改后,add, ...
- struts2中<jsp:forward>跳转时报404错误的问题
index.jsp页面: <jsp:forward page="show.action"></jsp:forward> 在struts.x ...