Blocks
Description
solution
这题和之前做过的一题的一个套路非常类似:把不是更优的决策给去掉,使得序列变得具有单调性,分析这题:
发现如果两个右端点 \(i\),\(j\) 满足 \(sum[j]<sum[i]\) 且 \(j<i\),那么 \(j\) 是不会进入最优决策的.
同理:如果两个左端点 \(i\),\(j\) 满足 \(sum[j]<sum[i]\) 且 \(i<j\) 那么 \(i\) 是不会进入最优决策的
所以我们分别维护一个左右端点的单调栈,然后两个单调指针扫一遍答案取Max即可
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=1000005;
inline int gi(){
RG int str=0;RG char ch=getchar();
while(ch>'9' || ch<'0')ch=getchar();
while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
return str;
}
int n,Q,a[N],st[N],q[N],tp=0;ll sum[N];
inline void solve(ll x){
int top=0,ans=0,tp=0;
q[++tp]=0;
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i]-x;
if(sum[i]<sum[q[tp]])q[++tp]=i;
}
for(int i=n;i>=1;i--){
if(!top || sum[i]>sum[st[top]])st[++top]=i;
}
for(int i=1;i<=tp;i++){
while(top>1 && sum[q[i]]<=sum[st[top-1]])top--;
if(q[i]<st[top] && sum[st[top]]-sum[q[i]]>=0)
ans=Max(ans,st[top]-q[i]);
}
printf("%d ",ans);
}
void work()
{
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++)a[i]=gi();
for(int i=1;i<=Q;i++)solve(gi());
}
int main()
{
work();
return 0;
}
Blocks的更多相关文章
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 开发该选择Blocks还是Delegates
前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...
- poj 1390 Blocks
poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...
- Java 同步代码块 - Synchronized Blocks
java锁实现原理: http://blog.csdn.net/endlu/article/details/51249156 The synchronized keyword can be used ...
- 区块 Blocks
Structure / Blocks / Demonstrate block regions
- 使用Code::blocks在windows下写网络程序
使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...
- Code::Blocks配置GTK+2和GTK+3
Code::Blocks配置GTK+2和GTK+3 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根 ...
- [翻译]理解Ruby中的blocks,Procs和lambda
原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...
- Java Synchronized Blocks
From http://tutorials.jenkov.com/java-concurrency/synchronized.html By Jakob Jenkov A Java synchro ...
随机推荐
- localhost访问不了的解决方法
c:\windows\system32\drivers\etc\hosts 用记事本打开,加入一行 127.0.0.1 localhost
- border 三角形 有边框的 东西南北的 气泡三角形
链接地址:http://www.cnblogs.com/blosaa/p/3823695.html
- TFTP通信原理
TFTP的通信流程 TFTP共定义了五种类型的包格式,格式的区分由包数据前两个字节的Opcode字段区分,分别是: · l 读文件请求包:Read request,简写为RRQ,对应Opcode字段值 ...
- Linux入门:usermod - 修改用户帐户信息
一.什么是usermod? usermod 命令通过修改系统帐户文件来修改用户账户信息usermod [options] user_name选项(options)-a|--append ##把用户追加 ...
- 新概念英语(1-107)It's Too Small.
Lesson 107 It's too small. 太小了. Listen to the tape then answer this question. What kind of dress doe ...
- SpringCloud应用入库后乱码问题
一.现象 1.请求 2.入库后 二.解决过程 1.配置application.properties 2.代码配置 3.数据库(关键!!) 3.请求 三.验证过程 1.win10 - 本地验证通过 2. ...
- SpringCloud的应用发布(四)vmvare+linux,网关代理
一.配置方式 1.代理同一个Eureka中注册的服务 2.代理url 二.访问方式:get - list 1.直接访问应用 2.代理访问应用
- angular2 学习笔记 ( animation 动画 )
refer : https://angular.io/guide/animations https://github.com/angular/angular/blob/master/packages/ ...
- linux下的Shell编程(6)case和select
第一个,除了if语句之外,Shell Script中也有类似C语言中多分支结构的case语句,它的语法是: case var in pattern 1 ) - ;; pattern 2 ) - ;; ...
- ios开发常识(1)开发语言和参考资料
学iphone开发用的语言是object-c,object-c和c++,java还是有很大区别,如果你坚持学习iphone开发的话可以不学c++,java,直接学习这个语言,先入为主,可能觉得这个语言 ...