题目连接:http://codeforces.com/problemset/problem/460/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 nm 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
2 2 2 2 1 1

output
2

input
2 5 1
5 8

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),从左到右排列,最多浇水m天,每天只能浇一次,每次可以使连续的 w 朵花的高度增加单位长度1,问最后m天浇完水后最矮的花的高度最高是达到多少。

思路:从n朵花中最矮min{ai}和最高max{ai}+m之间二分枚举高度。

代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#define MAX 100010
using namespace std;int n, m, w;  // n flowers, m days, w-width
int a[MAX];bool check(int mid)
{  // O(n)的时间处理 n-w+1 个宽为 w 的连续区间int grow[MAX];  // 每盆花的高度值int st[MAX];memset(grow,0,sizeof(grow));memset(st,0,sizeof(st));for (int i = 0; i < n; i++)grow[i] = a[i];int delta = 0;  int moves = m;for (int i = 0; i < n; i++){delta -= st[i];    // 除去对区间外浇花的影响grow[i] += delta;  // w-width区间内的花全部生长if (grow[i] < mid){int h = mid-grow[i];grow[i] = mid;delta += h;st[i+w<n?i+w:n] += h;  // O(1)时间moves -= h;if (moves < 0)return false;}}return true;
}int main()
{cin >> n >> m >> w;int left = 0x7fffffff, right = 0;memset(a,0,sizeof(a));for (int i = 0; i < n; i++){cin >> a[i];if (a[i] < left) left = a[i];if (a[i] > right) right = a[i];}right += m;int ans;while (left <= right){int mid = (left+right)>>1;if (check(mid)) {left = mid+1, ans = mid;} else right = mid-1;}cout << ans << endl;return 0;
}

最后想说的, check() 函数真的写得很巧妙,现在理解得也不是太透彻,是特别的数据结构吗?

每天一点点积累吧!

参考:

1. CodeforcesRound #262 (Div. 2) Editorial http://codeforces.ru/blog/entry/13465?locale=en

2. http://blog.csdn.net/accepthjp/article/details/38737429

Codeforces Round #262 (Div. 2) 460C. Present相关推荐

  1. Codeforces Round #262 (Div. 2) 460C. Present(二分)

    题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

  2. Codeforces Round #626 (Div. 2) D. Present 按位贡献 + 快排新姿势

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的序列aaa,让你计算 n≤4e5,a≤1e7n\le 4e5,a\le 1e7n≤4e5,a≤1e7 思路: 首先这个式子是n2n^2n ...

  3. Codeforces Round #262 (Div. 2)-A,B,C,D

    A. Vasya and Socks 水题就不用多说了,直接暴力枚举就完事了. #include <iostream> #include<stdio.h> #include&l ...

  4. Codeforces Round #739 (Div. 3) ABCDEF1F2 解题思路

    Codeforces Round #739 (Div. 3) 可能是一开始大佬都写F1去了,我在D写完后发现F过的人数比E多了好多(个位数与十位数),以为F1比较简单,就直接开F1了,但自己分类讨论老 ...

  5. Codeforces Round #706 (Div. 2)-B. Max and Mex-题解

    目录 Codeforces Round #706 (Div. 2)-B. Max and Mex Problem Description Input Output Sample Input Sampl ...

  6. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  7. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  8. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  9. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

最新文章

  1. 【Data】数据结构之C++程序设计(1)
  2. 抽象类,虚方法,接口
  3. ip地址管理与子网的划分二
  4. oracle12g安装手册,oracle 12c 安装 手册
  5. 数据字典怎么写_求职数据分析,项目经验该怎么写
  6. go 学习Printf
  7. The Network Adapter could not establish the connection解决
  8. Java中HttpClient设置超时时间
  9. 2019 十大国产开源项目来势汹汹!
  10. python删除指定日期前的备份文件
  11. Android零基础入门第56节:翻转视图ViewFlipper打造引导页和轮播图
  12. 综合实践活动信息技术小学版第三册电子课本_摆事实,讲道理!电子商务讲师证报名入口和费用...
  13. php中求10递归算法,PHP递归算法的应用(含示例)
  14. H2O学习笔记(一)—— H2O概述
  15. 操作系统学习笔记(二十二)~虚拟存储技术+请求分页+页面置换
  16. Airbnb暂停中国境内服务 中国民宿市场消化15万房源
  17. 事务的四大属性ACID即事务的原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability.。...
  18. 加餐 | Java 面试通关攻略
  19. 【苹果推相册软件】imessage群发arrangesAllSubviews安装
  20. BZOJ4399魔法少女LJJ——线段树合并+并查集

热门文章

  1. 【CSS】利用CSS伪类选择器实现三角形
  2. 分解质因数(质数分解)
  3. 用photoshop制作指定尺寸的证件照的方法
  4. 史上最污技术解读,60 个 IT 术语你能懂多少.
  5. 物联网5G工业级无线cpe路由器设备
  6. Mac OS自带游戏
  7. 自动生成VGG图像注释文件
  8. MATLAB--运用傅里叶变换对信号进行简单的滤波
  9. 数据采集实验-爬取李开复博客并保存在csv和mongodb中
  10. Photoshop文字之——打造立体的金属网格字