Problem L. Landscape Improved
Input file: landscape.in
Output file: landscape.out
Louis L Le Roi-Univers has ordered to improve the landscape that is seen from the royal palace. His
Majesty prefers to see a high mountain.
The Chief Landscape Manager is going to raise a mountain for Louis. He represents a landscape as a
flat picture on a grid of unit squares. Some of the squares are already filled with rock, while others are
empty. This greatly simplifies the design. Unit squares are small enough, and the landscape seems to be
smooth from the royal palace.
The Chief Landscape Manager has a plan of the landscape — the heights of all rock-filled columns for
each unit of width. He is going to add at most n square units of stones atop of the existing landscape to
make a mountain with as high peak as possible. Unfortunately, piles of stones are quite unstable. A unit
square of stones may be placed only exactly on top of the other filled square of stones or rock, moreover
the squares immediately to the bottom-left and to bottom-right of it should be already filled.
Existing landscape
Improved landscape
Your task is to help The Chief Landscape Manager to determine the maximum height of the highest
mountain he can build.
Input
The first line of the input file contains two integers w — the width of the existing landscape and n —
the maximum number of squares of stones to add (1 ≤ w ≤ 100 000, 0 ≤ n ≤ 1018).
Each of the following w lines contains a single integer hi — the height of the existing landscape column
(1 ≤ hi ≤ 109
).
Output
The output file shall contain the single integer — the maximum possible landscape height after at most
n unit squares of stones are added in a stable way.
Sample input and output
landscape.in landscape.out
8 4

3 100

题意:这题是给你w列方格,然后给你n个方块,让你加进去,使得这个图变得最高,加的要求是,如果这块的下面,以及左下右下都有,才能放

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int h[N],l[N],r[N];
ll sum[N];
int n; ll w; bool ok(int mid)
{
MM(r,inf);MM(l,0);
for(int i=1;i<=n;i++) {
ll a=i-(mid-h[i]),
b=i+(mid-h[i]);
if(a>=1) r[a]=min(r[a],i);
if(b<=n) l[b]=max(l[b],i);
}
for(int i=1;i<=n;i++) l[i]=max(l[i],l[i-1]);
for(int i=n;i>=1;i--) r[i]=min(r[i],r[i+1]);
for(int i=1;i<=n;i++) {
if(l[i]==0||r[i]==inf) CT;
ll all=0;
int k1=i-(l[i]+1)+1;
all+=((ll)k1)*mid-((ll)k1)*(k1-1)/2LL;
int k2=(r[i]-1)-i+1;
all+=((ll)k2)*mid-((ll)k2)*(k2-1)/2LL;
all-=mid;
if(all-(sum[r[i]-1]-sum[l[i]])<=w) return true;
}
return false;
} int main()
{
freopen("landscape.in","r",stdin);
freopen("landscape.out","w",stdout);
while(~SC("%d%lld",&n,&w))
{
int maxn=0;
MM(sum,0);
for(int i=1;i<=n;i++) {
SC("%d",&h[i]);
maxn=max(maxn,h[i]);
sum[i]=sum[i-1]+h[i];
}
int la=maxn,ra=2*1e9;
while(ra-la>1){
int mid=(la+(ll)ra)>>1;
if(ok(mid)) la=mid;
else ra=mid;
}
printf("%d\n",la);
}
return 0;
}

  题解链接:

TTTTTTTTTTTTTTTTTT Gym 100851L 看山填木块的更多相关文章

  1. BZOJ 3368 约翰看山(扫描)O(N)

    这题,简直丧心病狂了. 大意是给你一个环上一些覆盖的区间,让你求总覆盖长度. 非常坑的点是这个区间因为是个环,所以可能逆时针给你,也可能顺时针给你,你特别要注意.那么区分顺时针和逆时针的方法 就是,题 ...

  2. Gym - 100851L:Landscape Improved (二分+单调性)

    题意: 一个宽度为N的网格图,i上有h[i]高的方块.现在你有W个方块,问怎么放使得最终的最高点最高.   当一个格子的下方,左下方和右下方都有方块那么久可以把方块放到这个格子上.最左端和最右端不能放 ...

  3. 冰雪奇缘,白色世界:四个IT人的四姑娘山双桥沟游记

    去年9月初去了川西的稻城亚丁,体会了金色世界秋日童话,还写了一篇游记<从你的全世界路过-一群程序员的稻城亚丁游记>,也是得到了很多朋友和童鞋的点赞.今年11月初趁着周末的两天时间和朋友去了 ...

  4. 《Debug Hacks》和调试技巧【转】

    转自:https://blog.csdn.net/sdulibh/article/details/46462529 Debug Hacks 作者为吉冈弘隆.大和一洋.大岩尚宏.安部东洋.吉田俊辅,有中 ...

  5. 你都用python来做什么?

    首页发现话题   提问     你都用 Python 来做什么? 关注问题写回答     编程语言 Python 编程 Python 入门 Python 开发 你都用 Python 来做什么? 发现很 ...

  6. 爬虫--requests讲解

    什么是requests? Requests是用Python语言编写,基于urllib,采用Apache2 Licensed 开源协议的HTTP库.它比urllib更加方便,可以节约我们大量的工作,完全 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  9. 浅析Yii2的view层设计

    Yii2.0的view层提供了若干重要的功能:assets资源管理,widgets小组件,layouts布局... 下面将通过对Yii2.0代码直接进行分析,看一下上述功能都是如何实现的,当然细枝末节 ...

随机推荐

  1. Dijkstra算法——超~~详细!!

    Dijkstra算法_ ** 时隔多月,我又回来了!**_ 今天下午久违的又学了会儿算法,又重新学习了一遍Dijkstra,这是第三次重新学习Dijkstra(*以前学的都忘完了>_<*) ...

  2. __formart__

    __format__ 一.__format__ 自定制格式化字符串 date_dic = { 'ymd': '{0.year}:{0.month}:{0.day}', 'dmy': '{0.day}/ ...

  3. 【思维】Kenken Race

    题目描述 There are N squares arranged in a row, numbered 1,2,...,N from left to right. You are given a s ...

  4. 基于白名单的Payload

    利用 Msiexec 命令DLL反弹 Msiexec是Windows Installer的一部分.用于安装Windows Installer安装包(MSI),一般在运行Microsoft Update ...

  5. Web API与MVC控制器的区别

    Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...

  6. parseInt parseFloat Number三者转换的方式

    1.parseInt:从左到右检测字符串,若能先检测到数字,则将数字转换成整形,否则返回NaN. 2.parseFloat:从左到右检测字符串,若能先检测到数字,则将数字转换成浮点型,否则返回NaN. ...

  7. vuex数据传递的流程

    当组件修改数据的时候必须通过store.dispacth来调用actions中的方法. 当actions中的方法被触发的时候通过调用commit的方法来触发mutations里面的方法 mutatio ...

  8. Android SQLiteDatabase的使用

    package com.shawn.test; import android.content.ContentValues; import android.content.Context; import ...

  9. Linux 虚拟机扩容

    由于在装软件,原来的20G空间不够使用,需要扩容操作. 1.关闭虚拟机 2.点击编辑虚拟机设置 选中硬盘,添加,硬盘,推荐,确定大小,完成. 2.启动虚拟机 查看磁盘使用情况: [root@maste ...

  10. SSO单点登录 与 CAS

    本文转载自http://www.imooc.com/u/2245641/articles非常好的sso单点登录理解文章 作者: 常明,Java架构师 Web应用系统的演化总是从简单到复杂,从单功能到多 ...