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 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...
随机推荐
- 【BZOJ-1926】粟粟的书架 二分 + 前缀和 + 主席树
1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec Memory Limit: 552 MBSubmit: 616 Solved: 238[Submit][Statu ...
- 【spoj8222】Substrings
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- Metro-UI系统-1-tile标签
一 效果图 二 各个效果的详解 1,简单磁贴 <div class="tile" data-role="title"> <!--定义一个磁贴- ...
- Android成长日记-ListView
数据适配器:把复杂的数据(数组,链表,数据库,集合等)填充在指定的视图界面上 适配器的类型: ① ArrayAdapter(数组适配器):用于绑定格式单一的数据 数据源:可以是集合或数组 ① Simp ...
- PHP FastCGI RCE Vul
catalog . Introduction . nginx文件类型错误解析漏洞 . 针对直接公网开放的Fast-CGI攻击 . 通过FCGI API动态修改php.ini中的配置实现RCE 1. I ...
- iptables实现正向代理
拓扑图 实现目标 内网用户通过Firewall服务器(iptables实现)访问外网http服务 配置 #iptables iptables -t nat -A POSTROUTING -i eth0 ...
- C#读txt文件并写入二维数组中(txt数据行,列未知)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- python deep copy and shallow copy
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...
- django models auto_now和auto_now_add的区别
DataTimeField()中auto_now参数和auto_now_add参数区别: 前者添加或者修改的都为现在的时间,可以再次更新: 后者仅仅为添加时候的时间,不可更改.
- SQL Server的数据库连接的极限在哪儿?
在软件设计中,关于多层的设计,有一部份是有关数据库的. 设计上分成这样三层 客户端UI -- 应用服务器 -- 数据库服务器 有个说法是,可以在应用服务器这一层共享使用数据库连接池,从而减轻数据库服务 ...