单调栈求每个数在哪些区间是最值的经典操作。

  把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间。

  poj2796 弹出的时候更新答案即可

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<cmath>
  8. #include<map>
  9. #define ll long long
  10. using namespace std;
  11. const int maxn=,inf=1e9;
  12. int n,top,ansl,ansr;
  13. ll ans=-;
  14. int st[maxn],a[maxn];
  15. ll sum[maxn];
  16. void read(int &k)
  17. {
  18. int f=;k=;char c=getchar();
  19. while(c<''||c>'')c=='-'&&(f=-),c=getchar();
  20. while(c<=''&&c>='')k=k*+c-'',c=getchar();
  21. k*=f;
  22. }
  23. int main()
  24. {
  25. read(n);
  26. for(int i=;i<=n;i++)read(a[i]),sum[i]=sum[i-]+a[i];a[++n]=-;
  27. for(int i=;i<=n;i++)
  28. {
  29. for(;top&&a[i]<=a[st[top]];top--)
  30. {
  31. ll val=1ll*a[st[top]]*(sum[i-]-sum[st[top-]]);
  32. if(val>ans)ans=val,ansl=st[top-]+,ansr=i-;
  33. }
  34. st[++top]=i;
  35. }
  36. printf("%lld\n%d %d",ans,ansl,ansr);
  37. return ;
  38. }

  51nod1215 求出最大值对答案的贡献之和与最小值对答案的贡献之和相减即可。

  显然贡献为a[i]*(i-st[top])*(st1[top]-st[top-1]);

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<queue>
  7. #include<cmath>
  8. #include<map>
  9. #define ll long long
  10. using namespace std;
  11. const int maxn=,inf=1e9;
  12. int n,top1,top2;
  13. int a[maxn],b[maxn],st1[maxn],st2[maxn];
  14. ll mx,mn;
  15. void read(int &k)
  16. {
  17. int f=;k=;char c=getchar();
  18. while(c<''||c>'')c=='-'&&(f=-),c=getchar();
  19. while(c<=''&&c>='')k=k*+c-'',c=getchar();
  20. k*=f;
  21. }
  22. int main()
  23. {
  24. read(n);
  25. for(int i=;i<=n;i++)read(a[i]),b[i]=a[i];b[++n]=inf;
  26. for(int i=;i<=n;i++)
  27. {
  28. for(;top1&&a[i]<=a[st1[top1]];top1--)
  29. mn+=1ll*a[st1[top1]]*(i-st1[top1])*(st1[top1]-st1[top1-]);
  30. for(;top2&&b[i]>=b[st2[top2]];top2--)
  31. mx+=1ll*b[st2[top2]]*(i-st2[top2])*(st2[top2]-st2[top2-]);
  32. st1[++top1]=st2[++top2]=i;
  33. }
  34. printf("%lld\n",mx-mn);
  35. return ;
  36. }

51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)的更多相关文章

  1. 51nod 1215 数组的宽度(单调栈)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 题意: 思路: 计算出以第i个数为最大值的区间范围,l_max[i ...

  2. 51nod 1215 数组的宽度

    若一个数在一段区间内作为最大值存在,那么答案应该加上这个数 若一个数在一段区间内作为最小值存在,那么答案应该减去这个数 所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定, ...

  3. poj 2796 Feel Good单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case T ...

  4. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  5. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  6. 差异:后缀数组(wzz模板理解),单调栈

    因为涉及到对模板的理解,所以就着代码看会好一些. 让那些坚决不颓代码的人受委屈了. 我是对着wzz的板子默写的,可能不完全一样啊. 还有代码注释里都是我个人的理解,不保证正确,但欢迎指正. 可以有选择 ...

  7. poj 2796 Feel Good 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8753   Accepted: 2367 Case Ti ...

  8. POJ - 2796 Feel Good 单调递增栈+前缀和

    Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...

  9. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

随机推荐

  1. 「日常训练」School Marks(Codeforces Round 301 Div.2 B)

    题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...

  2. uvaoj1586Molar mass(暴力)

    An organic compound is any member of a large class of chemicalcompounds whose molecules contain carb ...

  3. tpo-10 C1 How to get photographs exhibited

    第 1 段 1.Listen to a conversation between a student and her Photography professor. 听一段学生和摄影学教授的对话. 第 ...

  4. <cerrno>

    文件头名称: <cerrno>(errno.h) 文件头描述: 文件内定义了如下的宏  errno 最后一个错误代码 加上其他至少的三个宏常量:EDOM,ERANGE 和EILSEQ 宏定 ...

  5. .NET中发送邮件的实现

    .NET中发送邮件 注意: 1.引用下列命名空间: using System.Net; using System.Net.Mail; 2.确保你使用的发送邮件的邮箱开启了stamp服务等. /// & ...

  6. Java应用基础微专业-入门篇

    第1章--用程序来做计算 1.1 第一个Java程序 Mac version: Preference -> General -> Keys -> Search "Conte ...

  7. 关于java中的“error: bad operand types for binary operator ”

    今天做这个题目的时候(142. O(1) Check Power of 2),遇到一个错误“ bad operand types for binary operator '&' ”. 先贴一下 ...

  8. 安卓客户端浏览器ajax注意

    这两天被一个bug搞疯了,就是公司安卓app上我负责的网页死活不进ajax,一开始我用的是post方式提交的,但是参数那一栏没写,直接把参数写在url上了,后来老大跟我说post不写参数会出问题,后来 ...

  9. kafka stream 低级别的Processor API动态生成拓扑图

    public class KafkaSream { public static void main(String[] args) { Map<String, Object> props = ...

  10. Python入门(5)

    导览: 函数 集合 迭代器与生成器 模块 一.函数 只要学过其他编程语言应该对函数不太陌生,函数在面向过程的编程语言中占据了极重要的地位,可以说没有函数,就没有面向过程编程,而在面向对象语言中,对象的 ...