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 ...
随机推荐
- 【Bootstrap】如何让响应式图片(img-responsive)水平居中
我们在用bootstrap排版内容的时候,有的时候在内容中需要图片水平居中对齐. 一般情况下,我们的图片都使用了 .img-responsive 类来实现响应式图片.如果需要实现响应式图片水平居中,那 ...
- centos7下安装python3 解决openssl等一系列问题
最近折腾了下centos7,发现按正常方法安装python3.7后面会出现各种操蛋的问题. 主要的问题有三个,openssl版本过低,'_ctypes'缺失,以及安装后sqlite3缺失.下面我会贴出 ...
- Linux 僵尸进程如何处理
Linux 允许进程查询内核以获得其父进程的 PID,或者其任何子进程的执行状态.例如,进程可以创建一个子进程来执行特定的任务,然后调用诸如 wait() 这样的一些库函数检查子进程是否终止.如果子进 ...
- 在oracle中将某个字段的数据作为列名的查询
原表结构 查询语句: select sno,sname,sum(语文) 语文,sum(数学) 数学,sum(英语) 英语 from (select sno,sname,decode(subjiect, ...
- 布尔类型、操作符别名、C++函数、动态内存分配(new\delete)、引用(day02)
六 C++的布尔类型 bool类型是C++中基本类型,专门表示逻辑值:true/false bool在内存上占一个字节:1表示true,0表示false bool类型可以接收任意类型和表达式的结果,其 ...
- 天翼云 RDS数据库操作
1.RDS数据库创建好之后点击RDS实例管理找到已下信息 官方文档 -1:http://www.ctyun.cn/help/qslist/567 官方文档 -2:http://www.ctyun.cn ...
- 我的网站恢复访问了,http://FansUnion.cn
博客网站 http://FansUnion.cn 恢复访问了. 周末,发表几篇原创文章.
- spring-boot 访问时,加与不加项目名分析
众所周知,springboot非常便捷,可以简化项目开启步骤,加快开发进度. 很多新手也许都遇到过这样一个问题,在以往的springMvc项目里,大家伙都是把打的war放在tomcat中运行,下意识的 ...
- 重命名文件及html
import os import nltk from bs4 import BeautifulSoup as bs def get_txt_name_from_bak_name(bak_name): ...
- 数据库工具——Navicat Premium使用技巧
Navicat Premium 常用功能讲解 1.快捷键 1.1. F8 快速回到当前对象列表 1.2. Ctrl + q 打开查询界面 1.3. Ctrl + d 快速修改当前的表结构 1.4 ...