Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented by S1, S2, S3, ...Sn. After measuring the length of each stick Sk (1 <= k <= n), she finds that for some sticks Si and Sj (1<= i < j <= n), each stick placed between Si and Sj is longer than Si but shorter than Sj.

Now given the length of S1, S2, S3, …Sn, you are required to find the maximum value j - i.

Input

The input contains multiple test cases. Each case contains two lines.
Line 1: a single integer n (n <= 50000), indicating the number of sticks.
Line 2: n different positive integers (not larger than 100000), indicating the length of each stick in order.

Output

Output the maximum value j - i in a single line. If there is no such i and j, just output -1.

Sample Input

4
5 4 3 6
4
6 5 4 3

Sample Output

1
-1 思路:求每个点往右的最大值且都不小于他,先想到了单调队列,区间最值就用ST表,整了整发现WA了,发现并不需要两端队列,遍历每一个点找右侧就行了,好像叫单调栈,找到比每个点小的第一个点,在这个区间内找最大点,用二分加速寻找就行了
const int maxm = 5e4+;

int Max[maxm][], Min[maxm][], a[maxm], N;

void init() {
memset(Max, , sizeof(Max)), memset(Min, , sizeof(Min));
} int BisearchMin(int pos) {
int l = pos+, r = N, mid, ans = N, k, t;
while(l <= r) {
mid = (l + r) >> ;
k = log((double)(mid-pos+)) / log(2.0);
t = min(Min[pos][k], Min[mid - ( << k) + ][k]);
if(t < a[pos]) {
ans = mid;
r = mid - ;
} else
l = mid + ;
}
return ans;
} int BisearchMax(int l, int r) {
int k, ans, mid, t, t2;
k = log((double)(r - l + )) / log(2.0);
t = max(Max[l][k], Max[r - (<<k)+][k]);
while(l <= r) {
mid = (l + r) >> ;
k = log((double)(mid - l + )) / log(2.0);
t2 = max(Max[l][k], Max[mid - (<<k)+][k]);
if(t2 == t) {
ans = mid;
r = mid - ;
} else
l = mid + ;
}
return ans;
} int main() {
while(scanf("%d", &N) != EOF) {
init();
int ans = -;
for(int i = ; i <= N; ++i) {
scanf("%d", &a[i]);
Max[i][] = Min[i][] = a[i];
}
for(int k = ; (<<k) <= N; ++k) {
for(int i = ; i+(<<k)- <= N; ++i) {
Max[i][k] = max(Max[i][k-], Max[i+(<<(k-))][k-]);
Min[i][k] = min(Min[i][k-], Min[i+(<<(k-))][k-]);
}
}
for(int i = ; i <= N; ++i) {
int r = BisearchMin(i);
r = BisearchMax(i, r);
if(i != r)
ans = max(ans, r - i);
}
printf("%d\n", ans);
}
return ;
}
												

Day6 - I - Sticks Problem POJ - 2452的更多相关文章

  1. POJ 2452 Sticks Problem

    RMQ+二分....枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素.....                   Sticks Problem Time Limit: 6000MS ...

  2. poj 2452(RMQ+二分查找)

    题目链接: http://poj.org/problem?id=2452 题意:在区间[1,n]上找到满足 a[i]<a[k]<a[j] (i<=k<=j) 的最大子区间 (j ...

  3. Sticks Problem

    Sticks Problem poj-2452 题目大意:给你一串n个数的数列a,上面的数为a1到an.我们求最大的y-x,其中,y和x满足1.x<y 2.任意的x<i<y,都有ai ...

  4. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  5. POJ_2452 Sticks Problem 【ST表 + 二分】

    一.题目 Sticks Problem 二.分析 对于$i$和$j$,并没有很好的方法能同时将他们两找到最优值,所以考虑固定左端点$i$. 固定左端点后,根据题意,$a[i]$是最小值,那么现在的问题 ...

  6. POJ 2452 Sticks Problem (暴力或者rmq+二分)

    题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i . 析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解 ...

  7. Jessica's Reading Problem POJ - 3320

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17562   Accep ...

  8. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  9. POJ-2452 Sticks Problem 二分+RMQ

    题目链接: https://cn.vjudge.net/problem/POJ-2452 题目大意: 给出一个数组a,求最大的j-i满足 i<j && a[i] ... a[j] ...

随机推荐

  1. vim功能之替换和查找

    vim有着强大的替换和查找功能,若能进行熟练的运用,可以让工作效率得到一个很大程度的提高. 替换 语法:[addr]s/源字符串/目的字符串/[option] [addr]表示检索范围,如: &quo ...

  2. 笔记-AJAX

    笔记-AJAX 1.      简介 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML AJAX 是一种用于创建快速动态网页的技术 ...

  3. git push的时候报错: Out of memory, malloc failed (tried to allocate 82037333 bytes)

    原因:上传的文件过大,这里我上传的文件有10G+所以报了上面的错误 解决方法:依次运行:git config --global pack.threads 1 git,git config --glob ...

  4. Vue——解决使用第三方组件库时无法修改默认样式的问题(使用 /deep/ )

    最近在开发一个基于Vue的后台管理系统,其中使用了element-ui第三方ui组件库.使用过组件库的人都知道,第三方组件往往会有一些默认的样式,而有些又是我们想要改变的. 一.基础(了解 <s ...

  5. @override编译报错

    今天突然遇到一个问题,明明我重写的接口的方法,编译的时候一直报@override is not override a method from superclass,查了一下资料,这个@override ...

  6. notepad++一次去掉所有空行,然后加上2个空行

    打开替换窗口,查找我的目标中填写: ^\r\n 替换为中不填,空着, 点击全部替换按钮. 如何给所有行添加2行空行: 打开替换窗口,查找目标中填写: \r\n 替换为中填写: \r\n\r\n\r\n ...

  7. 【PAT甲级】1032 Sharing (25 分)

    题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...

  8. ls命令与cd命令

    ls命令用于显示文件目录列表,当使用ls命令时,默认显示的只有非隐藏文件或文件夹(隐藏文件在linux中前面有 ' . ' ),当不加参数时,显示当前目录. 1.ls命令语法 ls [参数][目标文件 ...

  9. Array数组的方法总结

    1.检测数组 自从ECMAScript3作出规定后,就出现了确定某个对象是不是数组的经典问题.对于一个网页,或者一个全局作用域而言,使用instanceof操作符就能得到满意结果. if (value ...

  10. python爬虫处理在线预览的pdf文档

    引言 最近在爬一个网站,然后爬到详情页的时候发现,目标内容是用pdf在线预览的 比如如下网站: https://camelot-py.readthedocs.io/en/master/_static/ ...