CF460C Present (二分 + 差分数列)
Codeforces Round #262 (Div. 2) C
C. Present
time limit per test
2 seconds memory limit per test
256 megabytes input
standard input output
standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions. There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get? Input
The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Output
Print a single integer — the maximum final height of the smallest flower. Sample test(s)
Input
6 2 3 Output
2 Input
2 5 1 Output
9 Note
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test. |
题意:给出一排花的碉值,共有n盆,初始碉值为a1,a2,...,an。每天可以增加连续的w盆花的碉值1点,进行m天,求碉值最低的花的碉值最大值。
题解:二分答案,用差分数列O(n)判断是否可行。
二分答案,设当前答案为x,也就是碉值最低的话的碉值最大值为x。
从头到尾观察花,若a[i]<x,则对a[i]开头的w盆花怒浇(x-a[i])天,让其碉值达到x。让所有的a[i]都>=x。若怒浇的天数和小于等于m,则可行。
而这个怒浇操作可以用差分队列实现,差分数列介绍在这里有:http://www.cnblogs.com/yuiffy/p/3923018.html
因为差分数列b[i]=a[i]-a[i-1],则当前点的值为now,下一个点的值就为now+b[i+1]。差分数列的L到R全加D操作: b[L]+=x; b[R+1]-=x;(注意这题R可能会怒超边界,记得特殊处理一下或者数组开大点)
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
vector<int>v;
int n,m,w;
int a[];
int b[]; void update(int L, int R, int x){
b[L]+=x;
if(R+<n) b[R+]-=x;
} bool check(int x){
int i,j;
for(i=;i<n;i++){
b[i]=a[i]-a[i-];
}
int anow=a[];
int y=;
for(i=;i<n;i++){
if(anow<x){
update(i,i+w-,x-anow);
y+=x-anow;
if(y>m)break;
anow=x;
}
anow+=b[i+];
}
if(y>m)return ;
else return ;
} int main(){
int i,j,k,mi;
scanf("%d%d%d",&n,&m,&w);
mi=1e9+;
REP(i,n){
scanf("%d",&a[i]);
mi=min(a[i],mi);
}
int l,r,mid;
l=mi;r=mi+m;
while(l<=r){
mid=(r-l)/+l;
if(check(mid))l=mid+;
else r=mid-;
}
printf("%d\n",r);
return ;
}
CF460C Present (二分 + 差分数列)的更多相关文章
- hdu4970 Killing Monsters (差分数列)
2014多校9 1011 http://acm.hdu.edu.cn/showproblem.php?pid=4970 Killing Monsters Time Limit: 2000/1000 M ...
- UVALive 4119 Always an integer (差分数列,模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Always an integer Time Limit:3000MS M ...
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- JZYZOJ1454 NOIP2015 D2T3_运输计划 二分 差分数组 lca tarjan 树链剖分
http://172.20.6.3/Problem_Show.asp?id=1454 从这道题我充分认识到我的脑子里好多水orz. 如果知道了这个要用二分和差分写,就没什么思考上的难点了(屁咧你写了一 ...
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 洛谷 P1083 [ NOIP 2012 ] 借教室 —— 线段树 / 二分差分数组
题目:https://www.luogu.org/problemnew/show/P1083 当初不会线段树的时候做这道题...对差分什么不太熟练,一直没A,放在那儿不管... 现在去看,线段树就直接 ...
- UVA - 11478 Halum 二分+差分约束
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...
随机推荐
- Windows XP/Windows 7/Windows 8/Windows 10系统封装的另类教程和思路
如果是早些年,XP时代的Ghost封装,各种的封装工具和驱动只能安装工具满天飞,比如龙帝国,还有很早用C++写的忘了什么名字了,自由天空的,非常的多: 当时为什么要用Ghost和用这些驱动安装工具以及 ...
- 记一次rsync增量同步远程服务器文件
rsync remote shell 增量方式同步数据 rsync同步文件有两种方式,一种是daemon的方式(rsync daemon)另一种方式是通过远程shell方式(rsync remote ...
- 【问题】R文件报错原因及解决办法 (转)
错误如图.下面是几种解决方法(网上搜集的). 1.如果是导入项目出现这个问题,一般是R文件没有更新造成(据说导入项目,R不会自动更新).可以Project——clean以下,R文件会重新生成. 2.选 ...
- 关于git不区分文件名大小写的处理
今天遇到了git不区分文件名大小写的问题,一开始着实郁闷了一把. 处理办法: windows下在git中修改文件的大小写 git mv --force myfile MyFile 或者 git mv ...
- HDU 4857 逃生(反向拓扑排序)
传送门 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社 ...
- 大一上学期C语言学习心得总结
经过一个学期的C语言学习,大体算是在这个编程语言上入了门,能够通过一些代码解决特定的问题.当然,每次成功将问题转换成代码都小有激动,虽然只是在黑框上输出了一些数字或是字符串. 编程,虽然还不是很懂,但 ...
- 如何使用alt键+数字键盘上的数字键打出特殊符号
如何使用alt键+数字键盘上的数字键打出特殊符号 有时当我需要画示意图说明一个问题,但是苦于没有合适的符号,因此,这篇博文将简单介绍一些特殊的符号方便自己以及大家使用. 实现原理很简单:所有的字符(包 ...
- git-tag
标签可以针对某一时间点的版本做标记,常用于版本发布. 列出标签 $ git tag # 在控制台打印出当前仓库的所有标签 $ git tag -l 'v0.1.*' # 搜索符合模式的标签 打标签 g ...
- re正则表达式13_review of regex symbols
Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you ...
- JavaScript排序算法——堆排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...