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 ...
随机推荐
- 从0开始的LeetCode生活—001-Two Sum
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 16-TypeScript装饰器模式
在客户端脚本中,有一个类通常有一个方法需要执行一些操作,当我们需要扩展新功能,增加一些操作代码时,通常需要修改类中方法的代码,这种方式违背了开闭的原则. 装饰器模式可以动态的给类增加一些额外的职责.基 ...
- navicate连接不上阿里云mysql
一 用xshell连接进入服务器: 二 使用命令连接mysql mysql -uroot -p 三 更新权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDE ...
- vue jsx 使用指南
vue jsx 使用指南 vue jsx 语法与 react jsx 还是有些不一样,在这里记录下. let component = null // if 语句 if (true) { compone ...
- Angular组件——组件生命周期(二)
一.view钩子 view钩子有2个,ngAfterViewInit和ngAfterViewChecked钩子. 1.实现ngAfterViewInit和ngAfterViewChecked钩子时注意 ...
- R语言-离职率分析
案例:员工流失是困扰企业的关键因素之一,在这次的分析中我将分析以下内容: 对一些重要变量进行可视化及探索分析,收入,晋升,满意度,绩效,是否加班等方面进行单变量分析 分析员工流失的因素,探索各个变量的 ...
- 阿里云API网关(8)开发指南-SDK下载
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- 阿里云API网关(7)开发指南-API参考
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- 新概念英语(1-65)Not a Baby
新概念英语(1-65)Not a Baby Does Jill take the key to the front door? A:What are you going to do this even ...
- Lintcode373 Partition Array by Odd and Even solution 题解
[题目描述] Partition an integers array into odd number first and even number second. 分割一个整数数组,使得奇数在前偶数在后 ...