E - Skyscrapers (hard version)

题意是给出一个数组m我们要构造一种数组a满足以下的条件:
1,a[i]<=m[i]
2,a只能是先增后减或者单调,中间允许相等
在所以可行的数组里面选择一个数列和最大的输出
画图、分析后可得,如果区间最大值在哪个位置确定,整个区间取到区间和最大值的那种情况就唯一确定了,贪心策略是当前结点k到前面比他小的第一个位置i都变成当前的结点值然后将i作为新的k向前递推,向后也一样;
这样我们可以枚举每一个点作为整个序列的最大值,计算其区间和,记录所有情况里面最大的那个输出。裸的算法(对每个)是O(n2)O(n^2)O(n2)的,但是可以一顿预处理,变成差不多O(n)O(n)O(n)
对于任意一个点k设他前面比他小的点在i位置他后面比他小的点在j位置
对于i~j(不包括i,j)内的所有点可以取到最大值为m[k]
我们可以用单调栈预处理出每个点前面和后面比他小的第一个位置。之后预处理出这个点为右最高点的左边部分的总和,这个点为左最高点的右边部分的总和。这样以这个点为最大值就是左总和加右总和了。

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<functional>
using namespace std;
typedef long long LL;
inline LL read()
{LL kk=0,f=1;char cc=getchar();while(cc<'0'||cc>'9'){if(cc=='-')f=-1;cc=getchar();}while(cc>='0'&&cc<='9'){kk=(kk<<1)+(kk<<3)+cc-'0';cc=getchar();}return kk*f;
}
const int maxn=1000111;
LL n,a[maxn],pre[maxn],nex[maxn],sta[maxn],cst=0,ou[maxn];
LL sump[maxn],sumn[maxn];
void init()
{n=read();for(int i=1;i<=n;++i)a[i]=read();cst=0;sta[0]=pre[1]=0;sta[++cst]=1;//单调栈预处理每个点左右最小值位置for(int i=2;i<=n;++i){while(a[sta[cst]]>a[i]&&cst)cst--;pre[i]=sta[cst];sta[++cst]=i;}cst=0;sta[0]=nex[n]=n+1;sta[++cst]=n;for(int i=n-1;i>=1;--i){while(a[sta[cst]]>a[i]&&cst)cst--;nex[i]=sta[cst];sta[++cst]=i;}for(int i=1;i<=n;++i)sump[i]=sump[pre[i]]+(i-pre[i])*a[i];//预处理左边和for(int i=n;i>=1;--i)sumn[i]=sumn[nex[i]]+(nex[i]-i)*a[i];//预处理右边和
}
void gao(int x)//以x点为最大值,输出这个序列
{int ip,in;ip=in=x;while(ip>0){for(int i=pre[ip]+1;i<=ip;++i)ou[i]=a[ip];ip=pre[ip];}while(in<=n){for(int i=in;i<=nex[in]-1;++i)ou[i]=a[in];in=nex[in];}printf("%lld",ou[1]);for(int i=2;i<=n;++i)printf(" %lld",ou[i]);printf("\n");
}
int main()
{init();LL ma=1;for(int i=2;i<=n;++i){LL now=sump[i]+sumn[i]-a[i];//预处理的时候实际上将当前点同时包括在左区间和右区间里面了,所以减掉一个LL da=sump[ma]+sumn[ma]-a[ma];if(da<now)ma=i;}gao(ma);
}

E - Skyscrapers (hard version)相关推荐

  1. [codeforces 1313C1] Skyscrapers (easy version) 问的是谷,答的是峰

    Codeforces Round #622 (Div. 2)   比赛人数5752 [codeforces 1313C1] Skyscrapers (easy version)   问的是谷,答的是峰 ...

  2. Skyscrapers (hard version) CodeForces - 1313C2(单调栈)

    This is a harder version of the problem. In this version n≤500000 The outskirts of the capital are b ...

  3. Skyscrapers (easy version)CodeForces - 1313C1(暴力)

    This is an easier version of the problem. In this version n≤1000 The outskirts of the capital are be ...

  4. CodeForces - 1313C2 Skyscrapers (hard version)(单调栈+dp/分治)

    题目链接:点击查看 题目大意:给出 n 块连续的空地可以建造摩天大楼,政府有规定,每块地最高只能建 a[ i ] 的高度,同时每栋大楼需要满足一个规则,即每栋大楼的两侧不允许同时存在比自己高的大楼,输 ...

  5. Tensorflow安装问题: Could not find a version that satisfies the requirement tensorflow pip命令...

    引言: Tensorflow大名鼎鼎,这里不再赘述其为何物.这里讲描述在安装python包的时候碰到的"No matching distribution found for tensorfl ...

  6. Android Build.VERSION.SDK_INT

    Build.VERSION.SDK_INT   代表的操作系统的版本号 ,谷歌的解释大致翻译如下 当前在此硬件上运行的软件的SDK版本装置.当设备启动时,此值从不更改,但它可能当硬件制造商提供OTA更 ...

  7. This version of Android Studio cannot open this project, please retry with Android Studio 3.5 or new

    今天github 下载一个库 导入 as 提示 This version of Android Studio cannot open this project, please retry with A ...

  8. No cached version of com.android.tools.build:gradle:2.0.0 available for offline mode.

    异常场景 从AS2.0升级到2.1,重新编译工程后,抛出了如下异常 Error:A problem occurred configuring root project 'AndroidStudioPr ...

  9. Error:Could not download guava.jar (com.google.guava:guava:19.0): No cached version available for of

    今天从git导入demo 报错 Error:Could not download guava.jar (com.google.guava:guava:19.0): No cached version ...

  10. Error:CPACK_PACKAGE_VERSION does not match version provided by version.hpp header!

    环境:Cmake3.10+OpenCV2.4.13.5+CUDA9.2 Problem CMake Warning at cmake/OpenCVPackaging.cmake:23 (message ...

最新文章

  1. Yii的errorLog
  2. Java中int[]与Integer[]相互转化的方法
  3. 你看,公司状告员工不加班,居然还告赢了
  4. 在debian上安装vmware tools的问题
  5. 深度学习在物理层信号处理中的应用研究
  6. 【SQL Server】 SQL Server Management Studio不允许保存更改,组织保存要求解决方案
  7. python爬虫系统知识_网络爬虫基础知识(Python实现)
  8. 【Linux】Linux的信号量集
  9. MBR分区和GPT分区区别
  10. hit_training_20140403
  11. 大数据时代 无处安放的隐私数据
  12. 四方光电扬尘传感器PM3006助力打赢蓝天保卫战
  13. PC端、手机端在线预览文档组件react-file-viewer与npm构建内存溢出
  14. sqlserver语法
  15. 基带0day漏洞可攻击数百万部华为手机
  16. javascript原型图了解
  17. java.lang.NoSuchMethodError的解决办法
  18. codeup27978 除法算式(NOIP1995普及组第1题)
  19. Facebook 流量路由最佳实践:从公网入口到内网业务的全路径 XDP/BPF 基础设施
  20. 互联网平台思维与互联网思维(科技财经频道)

热门文章

  1. 慢慢的,就没有了,就像从未存在过
  2. css边框图片border-image切图原理
  3. 顺序表的基本操作(含全部代码c)
  4. 蓝牙定位技术原理--蓝牙人员定位--蓝牙定位--新导智能
  5. Raspberry Pi树莓派分类和其相似产品介绍
  6. Hi3516开发笔记(四):Hi3516虚拟机编译uboot、kernel、roofts和userdata以及分区表
  7. 【大数据】Linkis如何安装部署,及一些常见问题
  8. 最强大脑记忆曲线(6)——字词录入页设计
  9. linux 查看vcf文件,Linux生信练习4-vcf
  10. TypeScript学习日记