Description

solution

这题和之前做过的一题的一个套路非常类似:把不是更优的决策给去掉,使得序列变得具有单调性,分析这题:

发现如果两个右端点 \(i\),\(j\) 满足 \(sum[j]<sum[i]\) 且 \(j<i\),那么 \(j\) 是不会进入最优决策的.

同理:如果两个左端点 \(i\),\(j\) 满足 \(sum[j]<sum[i]\) 且 \(i<j\) 那么 \(i\) 是不会进入最优决策的

所以我们分别维护一个左右端点的单调栈,然后两个单调指针扫一遍答案取Max即可

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #define RG register
  8. #define il inline
  9. #define iter iterator
  10. #define Max(a,b) ((a)>(b)?(a):(b))
  11. #define Min(a,b) ((a)<(b)?(a):(b))
  12. using namespace std;
  13. typedef long long ll;
  14. const int N=1000005;
  15. inline int gi(){
  16. RG int str=0;RG char ch=getchar();
  17. while(ch>'9' || ch<'0')ch=getchar();
  18. while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
  19. return str;
  20. }
  21. int n,Q,a[N],st[N],q[N],tp=0;ll sum[N];
  22. inline void solve(ll x){
  23. int top=0,ans=0,tp=0;
  24. q[++tp]=0;
  25. for(int i=1;i<=n;i++){
  26. sum[i]=sum[i-1]+a[i]-x;
  27. if(sum[i]<sum[q[tp]])q[++tp]=i;
  28. }
  29. for(int i=n;i>=1;i--){
  30. if(!top || sum[i]>sum[st[top]])st[++top]=i;
  31. }
  32. for(int i=1;i<=tp;i++){
  33. while(top>1 && sum[q[i]]<=sum[st[top-1]])top--;
  34. if(q[i]<st[top] && sum[st[top]]-sum[q[i]]>=0)
  35. ans=Max(ans,st[top]-q[i]);
  36. }
  37. printf("%d ",ans);
  38. }
  39. void work()
  40. {
  41. scanf("%d%d",&n,&Q);
  42. for(int i=1;i<=n;i++)a[i]=gi();
  43. for(int i=1;i<=Q;i++)solve(gi());
  44. }
  45. int main()
  46. {
  47. work();
  48. return 0;
  49. }

Blocks的更多相关文章

  1. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 开发该选择Blocks还是Delegates

    前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...

  4. poj 1390 Blocks

    poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...

  5. Java 同步代码块 - Synchronized Blocks

    java锁实现原理: http://blog.csdn.net/endlu/article/details/51249156 The synchronized keyword can be used ...

  6. 区块 Blocks

    Structure / Blocks / Demonstrate block regions

  7. 使用Code::blocks在windows下写网络程序

    使用Code::blocks在windows下写网络程序 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创 ...

  8. Code::Blocks配置GTK+2和GTK+3

    Code::Blocks配置GTK+2和GTK+3 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根 ...

  9. [翻译]理解Ruby中的blocks,Procs和lambda

    原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...

  10. Java Synchronized Blocks

    From http://tutorials.jenkov.com/java-concurrency/synchronized.html By Jakob Jenkov   A Java synchro ...

随机推荐

  1. localhost访问不了的解决方法

    c:\windows\system32\drivers\etc\hosts 用记事本打开,加入一行 127.0.0.1       localhost

  2. border 三角形 有边框的 东西南北的 气泡三角形

    链接地址:http://www.cnblogs.com/blosaa/p/3823695.html

  3. TFTP通信原理

    TFTP的通信流程 TFTP共定义了五种类型的包格式,格式的区分由包数据前两个字节的Opcode字段区分,分别是: · l 读文件请求包:Read request,简写为RRQ,对应Opcode字段值 ...

  4. Linux入门:usermod - 修改用户帐户信息

    一.什么是usermod? usermod 命令通过修改系统帐户文件来修改用户账户信息usermod [options] user_name选项(options)-a|--append ##把用户追加 ...

  5. 新概念英语(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 ...

  6. SpringCloud应用入库后乱码问题

    一.现象 1.请求 2.入库后 二.解决过程 1.配置application.properties 2.代码配置 3.数据库(关键!!) 3.请求 三.验证过程 1.win10 - 本地验证通过 2. ...

  7. SpringCloud的应用发布(四)vmvare+linux,网关代理

    一.配置方式 1.代理同一个Eureka中注册的服务 2.代理url 二.访问方式:get - list 1.直接访问应用 2.代理访问应用

  8. angular2 学习笔记 ( animation 动画 )

    refer : https://angular.io/guide/animations https://github.com/angular/angular/blob/master/packages/ ...

  9. linux下的Shell编程(6)case和select

    第一个,除了if语句之外,Shell Script中也有类似C语言中多分支结构的case语句,它的语法是: case var in pattern 1 ) - ;; pattern 2 ) - ;; ...

  10. ios开发常识(1)开发语言和参考资料

    学iphone开发用的语言是object-c,object-c和c++,java还是有很大区别,如果你坚持学习iphone开发的话可以不学c++,java,直接学习这个语言,先入为主,可能觉得这个语言 ...