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 ...
随机推荐
- 使用File类操作文件或目录的属性
在学I/O流之前,我先总结一下使用File类操作文件或目录的属性. package com.File; import java.io.File; import java.io.IOException; ...
- 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环
7.1 函数input()的工作原理 函数input() 让程序暂停运行,等待用户输入一些文本.获取用户输入后,Python将其存储在一个变量中,以方便你使用. message = input(&qu ...
- 浅谈Overload和Override的区别
如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding).如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Over ...
- sql server 查询数据判断为空
and xxx is NOT null and xxx is null
- Git及Github环境搭建(Windows系统)
一.github账号注册 1.打开网址https://github.com 注册账号: 二.本地安装Git 1.安装包下载地址:链接:https://pan.baidu.com/s/1smpnJL7 ...
- node、Mongo项目如何前后端分离提供接口给前端
node接口编写,vue-cli代理接口方法 通常前端使用的MocK 数据的方法,去模拟假的数据,但是如果有node Mongodb 去写数据的话就不需要在去mock 数据了,具体的方法如下. 首先 ...
- JAVA中浅复制与深复制
1.浅复制与深复制概念⑴浅复制(浅克隆)被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象. ⑵深复 ...
- jsp 判断当前时间是否符合设置的时间条件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- eas更改用户组织范围和业务组织范围
表: T_PM_OrgRangeIncludeSubOrg 10 20 30 分别代表 业务组织 行政组织 以及管辖组织.查行政组织,
- Codeforces Round #547 (Div. 3) E. Superhero Battle
E. Superhero Battle A superhero fights with a monster. The battle consists of rounds, each of which ...