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. C# trim split dataGrid

    C#中Trim().TrimStart().TrimEnd()的用法:     这三个方法用于删除字符串头尾出现的某些字符.Trim()删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一 ...

  2. (全然背包)小P寻宝记——好基友一起走

    题目描写叙述 话说.上次小P到伊利哇呀国旅行得到了一批宝藏.他是相当开心啊.回来就告诉了他的好基友小鑫.于是他们又结伴去伊利哇呀国寻宝. 这次小P的寻宝之路可没有那么的轻松,他们走到了一个森林,小鑫一 ...

  3. LeetCode求能够装得最多的水

    费了半天劲还是没想出来,然后跑到网上找答案,明白了是怎么回事儿了,就是不知道为啥自己没有想出来. 明天再搞~~~ 国庆快乐~~~ 一年了~~~~~ 明年今日~~我会在哪儿 =====2017.9.30 ...

  4. MySQL字段名与保留字冲突的问题及解决方法

    问题:MySQL字段名与保留字冲突在实际操作是常常出现的.一把会出现下面错误. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException. 解 ...

  5. stretchableImageWithLeftCapWidth

    本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...

  6. ios 使用自定义字体

    本文转载至 http://blog.csdn.net/yesjava/article/details/8447596   1.下载要使用的自定义字体,格式通常为ttf.otf文件.这里假设是nokia ...

  7. EasyIPCamera实现Windows PC桌面、安卓Android桌面同屏直播,助力无纸化会议系统

    最近在EasyDarwin开源群里,有不少用户私信需求,要做一种能够多端同屏的系统,细分下来有屏幕采集端和同屏端,屏幕采集端细想也就是一个低延时的流媒体音视频服务器,同屏端也就是一个低延时的播放器,负 ...

  8. 大数据之ES系列——第一篇 ElasticSearch2.2 集群安装部署

    第一部分  安装准备 准备三台主机节点: hc11.spads  192.168.160.181 hc12.spads  192.168.160.182 hc13.spads  192.168.160 ...

  9. Android图片加载神器之Fresco, 基于各种使用场景的讲解

    Fresco是Facebook开源Android平台上一个强大的图片加载库,也是迄今为止Android平台上最强大的图片加载库. 优点:相对于其他开源的第三方图片加载库,Fresco拥有更好的内存管理 ...

  10. bootstrap 学习笔记(5)---- 图片和响应式工具

    (一)响应式图片: 在 Bootstrap 版本 3 中,通过为图片添加 .img-responsive 类可以让图片支持响应式布局.其实质是为图片设置了 max-width: 100%;. heig ...