51nod 1349 最大值(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1349
题意:
求区间内最大值大于等于k的区间个数。
思路:
利用求出对于以a[i]为最大值的区间范围,pre[i]表示左端范围,aft[i]表示右端范围,则区间个数为$(i-pre[i]+1)*(aft[i]-i+1)$。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n;
int a[maxn];
int sta[maxn];
ll ans[maxn]; ll pre[maxn],aft[maxn]; inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++) a[i]=read();
int top = ;
for(int i=;i<=n;i++)
{
while(top && a[sta[top]]<=a[i]) top--;
if(top==) pre[i]=;
else pre[i]=sta[top]+;
sta[++top]=i;
}
top=;
for(int i=n;i>=;i--)
{
while(top && a[sta[top]]<a[i]) top--; //这儿特别注意一下
if(top==) aft[i]=n;
else aft[i]=sta[top]-;
sta[++top]=i;
}
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++) ans[a[i]]+=(i-pre[i]+)*(aft[i]-i+);
for(int i=;i>=;i--) ans[i]+=ans[i+];
int q; q=read();
while(q--)
{
int x; x=read();
printf("%lld\n",ans[x]);
}
}
return ;
}
51nod 1349 最大值(单调栈)的更多相关文章
- 51nod 1437 迈克步 单调栈
利用单调栈高效的求出,一个数a[i]在哪个区间内可作为最小值存在. 正向扫描,求出a[i]可做为最小值的区间的左边界 反向扫描,求出a[i]可作为最小值的区间的右边界 r[i] - l[i] +1 就 ...
- 51nod 1102 【单调栈】
思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...
- 51nod 1349 最大值
题目看这里 找到每个元素g[i]作为最大值的区间[L,R],那么以他为最大值的区间数有(i-L+1)*(R-i+1)个. 为了加速,以k为最大值的区间数放入H[k],再以此统计一个前缀和,更新入H.那 ...
- 51nod 1437 迈克步——单调栈
有n只熊.他们站成一排队伍,从左到右依次1到n编号.第i只熊的高度是ai. 一组熊指的队伍中连续的一个子段.组的大小就是熊的数目.而组的力量就是这一组熊中最小的高度. 迈克想知道对于所有的组大小为x( ...
- 51nod 1437 迈克步(单调栈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1437 题意: 思路: 单调栈题.求出以每个数为区间最大值的区间范围即可. ...
- 51nod 1215 单调栈/迭代
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 数组的宽度 题目来源: Javaman 基准时间限制:1 ...
- 51nod 1102 单调栈
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 1102 面积最大的矩形 基准时间限制:1 秒 空间限制:1310 ...
- HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)
单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
- 51nod 1423 最大二“货” 单调栈
利用单调栈,高效求出每个区间内的最大值和次大值的亦或值. 先正向扫描,利用单调递减栈,若当前栈为空栈,则直接压入栈中,若为非空栈,弹出栈顶元素,每弹出一个元素,则求一次亦或值,保留最大值 接着进行反向 ...
随机推荐
- Linux服务器安装部署redis
参考地址: redis教程:http://www.runoob.com/redis/redis-tutorial.html redis百度百科:https://baike.baidu.com/item ...
- pyspider操作千万级库,pyspider在对接量级较大库的策略
pyspider操作千万级库,pyspider在对接量级较大库的策略 如果是需要pyspider正常的流程去执行,那必然是会在on_strat()时任务执行超时,可能只读取出几万条或十几万条数据就会被 ...
- UART接口与COM口的区别
原文地址:https://blog.csdn.net/wordwarwordwar/article/details/78883732 简单的讲:(UART与COM) 嵌入式里面说的串口,一般是指UAR ...
- JDBC-day01
#JDBC Java DataBase Connectivity Java数据库连接 JDBC提供了一套和数据库交互 ###为什么要使用JDBC 因为Java语言在工作中有可能会有需求去访问各种数据库 ...
- Django MTV simple_tag filter inclusion_tag
Django框架 模型(Model).视图(View)和控制器(Controller),具有耦合性低.重用性高.生命周期成本低等优点. MVC 框架 -- Model -View -Controll ...
- python 图片
- UVALive 3295 Counting Triangles
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- python colorama模块
colorama是一个python专门用来在控制台.命令行输出彩色文字的模块,可以跨平台使用. 1. 安装colorama模块 pip install colorama 可用格式常数: Fore: B ...
- 听 Fabien Potencier 谈Symfony2 之 《What is Symfony2 ?》
Symfoy2 是什么? PHP世界里又一广受关注的web MVC框架? Fabien Potencier 却不这么说! Fabien Potencier这样定义Symfoy2 是个什么东西: 首先, ...
- 【javascript】对原型对象、原型链的理解
原型对象,原型链这些知识属于基础类知识.但是平时开发过程中也很少用到. 看网上的意思,原型链用于es5开发场景下的继承.es6有了类语法糖之后,就自带继承了. 通过理解,个人画了一张原型链解构的关系图 ...