题意:1e5个数围成一个环。现在要输出每个数左右第一个大于它的数的下标。若没有,则输出-1.

题解:单调栈板题。只是要把数据压入栈压两遍来模仿环。

     具体分析:考虑一个递减的数列。要找左边最大的数,我们从左到右对于每一个数,只要往回枚举一步就能找到,时间复杂度为O(n);

        考虑一个递增的数列。同样用暴力枚举,每次都要找到头(尾),复杂度为O(n*n); 如何优化?

        我们发现在递增数列的例子中如果a[N]一直遍历到头才停止,那么只要a[N+1]>a[N],就可以直接跳过之前的n-1次遍历。

        那如何记录“一直遍历到某点才停止”呢?

        一种方法是写一个数组,记录可以走到最远的地方有点记忆递归的意思。

        另一种就是把遍历过的数全删光,这就是单调栈的思想。

        

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
//std::ios::sync_with_stdio(false);
using namespace std; const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
typedef unsigned long long ull; stack<int>s;
deque<int>l, r;
int h[maxn];
#define ONLINE_JUDGE
int main() { #ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int t; cin >> t; while (t--) {
int n;
while (!s.empty())s.pop();
l.clear(), r.clear(); cin >> n;
int mx = , mxi = ;
for (int i = ; i <= n; i++) {
scanf("%d", &h[i]); while (!s.empty() && h[s.top()] <= h[i])s.pop(); s.push(i);//存 个下降子序列 末位是最高的那个
}
for (int i = ; i <= n; i++) {
while (!s.empty() && h[s.top()] <= h[i])s.pop();
if (!s.empty())l.push_back(s.top());
else l.push_back(-);
s.push(i);
}
while (!s.empty())s.pop();
for (int i = n; i > ; i--) {
while (!s.empty() && h[s.top()] <= h[i])s.pop(); s.push(i);
}
for (int i = n; i > ; i--) {
while (!s.empty() && h[s.top()] <= h[i])s.pop();
if (!s.empty())r.push_front(s.top());
else r.push_front(-);
s.push(i);
}
int sz = l.size();
for (int i = ; i < sz; i++) {
printf("%d %d\n", l[i], r[i]);
}
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}

Fata7y Ya Warda! SPOJ - DRUIDEOI 单调栈的更多相关文章

  1. Spoj-DRUIDEOI Fata7y Ya Warda!

    Fata7y Ya Warda! Druid (AKA Amr Alaa El-Deen) and little EOIers have finished their training and the ...

  2. spoj MINSUB 单调栈+二分

    题目链接:点击传送 MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative i ...

  3. SPOJDRUIDEOI - Fata7y Ya Warda!【单调栈】

    题目链接[http://www.spoj.com/problems/DRUIDEOI/en/] 题意:给出n个数,从1到n围城一个环(1和n相连),求每个数左边第一个比他大的第一个下标,右边第一个比他 ...

  4. SPOJ MINSUB - Largest Submatrix(二分+单调栈)

    http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...

  5. 「日常训练&知识学习」单调栈

    这几天的知识学习比较多,因为时间不够了.加油吧,为了梦想. 这里写几条简单的单调栈作为题解记录,因为单调栈的用法很简单,可是想到并转化成用这个需要一些题目的积淀. 相关博客参见:https://blo ...

  6. HDU - 5033 Building (单调栈+倍增)

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

  7. Codeforces 1156E Special Segments of Permutation(单调栈)

    可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...

  8. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  9. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

随机推荐

  1. flexbox子盒子flex属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. RPM常用命令解释

    RPM软件包管理器,英文:RPM Package Manager(原Red Hat Package Manager,现在是一个递归缩写) -i安装rpm包 -u升级rpm包 -q查询已安装的软件信息 ...

  3. selenium之 chromedriver与chrome版本映射表(更新至v2.31)

    转自:http://blog.csdn.net/huilan_same/article/details/51896672 chromedriver版本 支持的Chrome版本 v2.31 v58-60 ...

  4. Django 添加应用

    一个项目可以添加多个应用,可以使用以下两种方法来添加应用: [root@localhost web]$ python manage.py startapp blog [root@localhost w ...

  5. mybatis 之resultType="Map"

    Map map = new HashMap(); map.put("productTypeID", productTypeId); List<HashMap> prod ...

  6. mybatis 之parameterType="Long"

    <select id="selectByPrimaryKeyByArrayMemberId" resultType="memberModel" param ...

  7. 【Linux】将终端的命令输出保存为txt文本文件

    Linux中的终端很方便,可以直接复制粘贴的. 之后开一个gedit文本编辑器,把复制到的内容粘贴就可以的. 不像windows的cmd控制台,需要先右键标题栏,选择编辑->全选/标记,在右键标 ...

  8. apache下配置php环境

    1. apache下载 http://httpd.apache.org/download.cgi 2. php下载 http://windows.php.net/download/ 3. 配置 apa ...

  9. Could not find the main class: org.apache.catalina.startup.Bootstrap. Program will exit.

    出现此异常原因是jdk环境变量未配置正确

  10. Eclipse新建动态web工程项目出现红叉解决方案

    问题描述:之前新建动态web工程一直没有问题,今天新建一个项目后项目名称上突然出现小红叉,子目录文件没有红叉. 解决过程:一开始想到的就是编译器的level设置,调整了一下,仍然没有解决. 然后在标记 ...