51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)
单调栈求每个数在哪些区间是最值的经典操作。
把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间。
poj2796 弹出的时候更新答案即可
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<algorithm>
- #include<queue>
- #include<cmath>
- #include<map>
- #define ll long long
- using namespace std;
- const int maxn=,inf=1e9;
- int n,top,ansl,ansr;
- ll ans=-;
- int st[maxn],a[maxn];
- ll sum[maxn];
- void read(int &k)
- {
- int f=;k=;char c=getchar();
- while(c<''||c>'')c=='-'&&(f=-),c=getchar();
- while(c<=''&&c>='')k=k*+c-'',c=getchar();
- k*=f;
- }
- int main()
- {
- read(n);
- for(int i=;i<=n;i++)read(a[i]),sum[i]=sum[i-]+a[i];a[++n]=-;
- for(int i=;i<=n;i++)
- {
- for(;top&&a[i]<=a[st[top]];top--)
- {
- ll val=1ll*a[st[top]]*(sum[i-]-sum[st[top-]]);
- if(val>ans)ans=val,ansl=st[top-]+,ansr=i-;
- }
- st[++top]=i;
- }
- printf("%lld\n%d %d",ans,ansl,ansr);
- return ;
- }
51nod1215 求出最大值对答案的贡献之和与最小值对答案的贡献之和相减即可。
显然贡献为a[i]*(i-st[top])*(st1[top]-st[top-1]);
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<algorithm>
- #include<queue>
- #include<cmath>
- #include<map>
- #define ll long long
- using namespace std;
- const int maxn=,inf=1e9;
- int n,top1,top2;
- int a[maxn],b[maxn],st1[maxn],st2[maxn];
- ll mx,mn;
- void read(int &k)
- {
- int f=;k=;char c=getchar();
- while(c<''||c>'')c=='-'&&(f=-),c=getchar();
- while(c<=''&&c>='')k=k*+c-'',c=getchar();
- k*=f;
- }
- int main()
- {
- read(n);
- for(int i=;i<=n;i++)read(a[i]),b[i]=a[i];b[++n]=inf;
- for(int i=;i<=n;i++)
- {
- for(;top1&&a[i]<=a[st1[top1]];top1--)
- mn+=1ll*a[st1[top1]]*(i-st1[top1])*(st1[top1]-st1[top1-]);
- for(;top2&&b[i]>=b[st2[top2]];top2--)
- mx+=1ll*b[st2[top2]]*(i-st2[top2])*(st2[top2]-st2[top2-]);
- st1[++top1]=st2[++top2]=i;
- }
- printf("%lld\n",mx-mn);
- return ;
- }
51nod 1215 数组的宽度&poj 2796 Feel Good(单调栈)的更多相关文章
- 51nod 1215 数组的宽度(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 题意: 思路: 计算出以第i个数为最大值的区间范围,l_max[i ...
- 51nod 1215 数组的宽度
若一个数在一段区间内作为最大值存在,那么答案应该加上这个数 若一个数在一段区间内作为最小值存在,那么答案应该减去这个数 所以我们利用单调栈,高效求出a[i]在哪个区间内作为最大/最小值存在,从而确定, ...
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
- POJ 3658 Artificial Lake (单调栈)
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...
- 差异:后缀数组(wzz模板理解),单调栈
因为涉及到对模板的理解,所以就着代码看会好一些. 让那些坚决不颓代码的人受委屈了. 我是对着wzz的板子默写的,可能不完全一样啊. 还有代码注释里都是我个人的理解,不保证正确,但欢迎指正. 可以有选择 ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- POJ - 2796 Feel Good 单调递增栈+前缀和
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
随机推荐
- 「日常训练」School Marks(Codeforces Round 301 Div.2 B)
题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...
- uvaoj1586Molar mass(暴力)
An organic compound is any member of a large class of chemicalcompounds whose molecules contain carb ...
- tpo-10 C1 How to get photographs exhibited
第 1 段 1.Listen to a conversation between a student and her Photography professor. 听一段学生和摄影学教授的对话. 第 ...
- <cerrno>
文件头名称: <cerrno>(errno.h) 文件头描述: 文件内定义了如下的宏 errno 最后一个错误代码 加上其他至少的三个宏常量:EDOM,ERANGE 和EILSEQ 宏定 ...
- .NET中发送邮件的实现
.NET中发送邮件 注意: 1.引用下列命名空间: using System.Net; using System.Net.Mail; 2.确保你使用的发送邮件的邮箱开启了stamp服务等. /// & ...
- Java应用基础微专业-入门篇
第1章--用程序来做计算 1.1 第一个Java程序 Mac version: Preference -> General -> Keys -> Search "Conte ...
- 关于java中的“error: bad operand types for binary operator ”
今天做这个题目的时候(142. O(1) Check Power of 2),遇到一个错误“ bad operand types for binary operator '&' ”. 先贴一下 ...
- 安卓客户端浏览器ajax注意
这两天被一个bug搞疯了,就是公司安卓app上我负责的网页死活不进ajax,一开始我用的是post方式提交的,但是参数那一栏没写,直接把参数写在url上了,后来老大跟我说post不写参数会出问题,后来 ...
- kafka stream 低级别的Processor API动态生成拓扑图
public class KafkaSream { public static void main(String[] args) { Map<String, Object> props = ...
- Python入门(5)
导览: 函数 集合 迭代器与生成器 模块 一.函数 只要学过其他编程语言应该对函数不太陌生,函数在面向过程的编程语言中占据了极重要的地位,可以说没有函数,就没有面向过程编程,而在面向对象语言中,对象的 ...