C - Present

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 wcontiguous 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 Input

Input
6 2 3
2 2 2 2 1 1
Output
2
Input
2 5 1
5 8
Output
9

先说题意:共有n盆花列为一排,输入n盆花的现高度,可浇水m天,每天只能浇一次,每次浇水只能浇相邻的w株,每浇水一次花长高1,求m天后最矮的花最高为多少。

可用二分答案的方法,将可能的高度二分,再比较在m天内能不能达到二分的数值,若能则继续向上二分答案,若不能则向下二分答案,直到找出最合适的值。

附AC代码:

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std; const int MAX=;
long long a[MAX],b[MAX],v[MAX]; int main(){
int n,m,w;
while(~scanf("%d %d %d",&n,&m,&w)){
long long low=1e9,top=-;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
if(a[i]<low)
low=a[i];
if(a[i]>top)
top=a[i];
}
top+=m;
long long mid,ans=-;
while(top>=low){
mid=(top+low)/;//中点
for(int i=;i<=n;i++)
b[i]=max(mid-a[i],(long long));//到达中点所需要的天数
memset(v,,sizeof(v));//v数组用来实现连续浇水w
long long day=m;//day表示天数
long long c=;//已浇水天数
for(int i=;i<=n;i++){
c+=v[i];//w个后c归零
b[i]-=c;//已浇c天
if(b[i]>){
day-=b[i];
if(day<)//天数不够
break;
else
c+=b[i];
v[i+w]-=b[i];//当i循环到w个后时,这些花之前并没有被浇水,所以减去,由循环开头式子得c=0
b[i]=;//已交够水了所以为0
}
}
if(day<){//当天数不够时向下二分答案
top=mid-;
}
else{//继续向上二分
ans=mid;
low=mid+;
}
}
printf("%I64d\n",ans);
}
return ;
}

C - Present的更多相关文章

  1. 跳转时候提示Attempt to present on while a presentation is in progress

    出现这种情况,例如:我在获取相册图片后,直接present到另一个页面,但是上一个页面可能还未dismiss,所以,要在获取相册图片的dismiss方法的complete的block里面写获取图片及跳 ...

  2. find your present (感叹一下位运算的神奇)

    find your present (2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. HTTP Status 400 - Required String parameter 'userName' is not present 错误

    HTTP Status 400 - Required String parameter 'userName' is not present 错误 先mark  有时间详细写 参考链接: https:/ ...

  4. Linux 克隆虚拟机引起的“Device eth0 does not seem to be present, delaying initialization”

    虚拟机Vmware上克隆了一个Red Hat Enterprise Linx启动时发现找不到网卡,如下所示,如果你在命令窗口启动网络服务就会遇到"Device eth0 does not s ...

  5. required string parameter XXX is not present

    @RequestParam jQuery调用方式: deleteFile: function(filePath) { return ajax({ method: 'POST', url: '/cm/s ...

  6. 启动网卡报:Device eth0 does not seem to be present”解决办法

    Device eth0 does not seem to be present”解决办法 : 用ifconfig查看发现缺少eth0,只有lo:用ifconfig -a查看发现多出了eth1的信息. ...

  7. jQuery 跨域访问的三种方式 No 'Access-Control-Allow-Origin' header is present on the reque

    问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present ...

  8. js跨域访问,No 'Access-Control-Allow-Origin' header is present on the requested resource

    js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is prese ...

  9. sudo: no tty present and no askpass program specified(转)

    sudo: no tty present and no askpass program specified 2012-11-30 09:30 5040人阅读 评论(1) 收藏 举报 修改sudo配置文 ...

  10. 虚拟机解决Device eth0 does not seem to be present 问题。

    Device eth0 does not seem to be present... 出现这个问题基本上是因为虚拟机是克隆的导致机器的mac网卡不一致,所以系统识别网卡失败:

随机推荐

  1. 手写JQuery 的框架的实现

    JQuery的好处 快速上手(学习成本低) 开发效率高(选择器.批量操作 DOM.链型操作--) 一系列的封装(动画.ajax) 浏览器兼容(1.x版本 兼容IE6.7.8) JQuery 1.11. ...

  2. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  3. 为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

    为了发布博客方便,现从CSDN的naedzq整体迁移到cnblogs

  4. MapReduce 1工作原理图文详解

    MapReduce工作原理图文详解 一 MapReduce程序执行流程 程序执行流程图如下: 流程分析:1.在客户端启动一个作业.2.向JobTracker请求一个Job ID.3.将运行作业所需要的 ...

  5. java_类型强转

    class Father{ public void fromFather(){ System.out.println("fromFather"); } } interface in ...

  6. SAP-ABAP系列 第二篇SAP ABAP开发基础

    第二章SAP ABAP开发基础 1.ABAP数据类型及定义 ABAP程序中共包含8种基本数据类型定义, 类型名称 描述 属性 C Character Text (字符类型) 默认长度=1,默认值 = ...

  7. Edit conflicts

    Edit conflicts 当副本修改处和服务器版本相同处被修改并下载到本地时,就会发生文件冲突. 操作步骤如下所示: Ø 执行"SVN Update" Ø 若发生冲突,会出现如 ...

  8. IOS下SQLite的简单使用

    本文转载至 http://www.cnblogs.com/cokecoffe/archive/2012/05/31/2537105.html 看着国外网站的教程,写了一个小例子,一个联系人的程序,包括 ...

  9. 【BZOJ4950】lydsy七月月赛 C 二分图最大匹配

    [BZOJ4950]lydsy七月月赛 C 题面 题解:比较直接的想法就是:每行,每列的最大值都留下,剩下的格子都变成1.但是如果一个格子既是行的最大值又是列的最大值,那么我们只需要把它留下即可.这就 ...

  10. SAM4E单片机之旅——1、LED闪烁之空循环

    最近因为导师要写一本关于SAME4单片机的书籍,而我也作为一个嵌入式的初学者看了这本书.现在也让我写写几个小的程序,做做示例.既然写了文档之类的,就发到博客上来吧. 目前关于这芯片能参考的书籍大概就只 ...