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. DataTable和实体类之间的转换

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  2. 爬虫模拟cookie自动登录(人人网自动登录)

    什么是cookie? 在网站中,HTTP请求时无状态的,也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是谁,cookie的出现就是为了解决这个问题,第一次登陆后服 ...

  3. 吴裕雄--天生自然ORACLE数据库学习笔记:管理控制文件和日志文件

    alter database add logfile ('D:\OracleFiles\LogFiles\REDO4_A.LOG', 'E:\OracleFiles\LogFiles\REDO4_B. ...

  4. Linux运维工程师简历项目经验

    如何做好一个合格的运工程师,运维工程师前景怎么样呢?就这些问题,与大家交流一下.首先对于运维工程师的要求是十分严苛的了,运维工程师不但要针对不同的问题做出响应,而且需要不断的补充自己的知识面,并不继提 ...

  5. redis 高级学习和应用场景

    redis 高级学习 1.redis 复制 2.redis 集群 3.哨兵机制 4.spring 与哨兵结合 5.数据恢复与转移 6.redis 的阻塞分析 redis 实战 1. 数据缓存(热点数据 ...

  6. Spark教程——(9)Oozie编排Spark任务

    进入Hue管理界面,打开Oozie Editor: 将打包好的Spark程序上传到HDFS上,拖拽Spark任务,编辑任务属性,选择打包好的Spark程序,设置主函数所在类,设置选项参数: 保存为任务 ...

  7. nacos集群配置

    一.    环境准备 Nacos 依赖 java环境来运行.如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用: 64 bit OS,支持 Lin ...

  8. Languages-used-on-the-Internet

    Languages-used-on-the-Internet 1. 互联网上使用的语言 1.1 网站内容语言 1.2 按语言互联网用户 1.3 维基百科文章统计 2. 综合以上表格数据出图表(2019 ...

  9. 【JAVA随摘笔记一】进制转换

    // 十进制转其它进制(二进制,八进制,十六进制) ; System.out.println(Integer.toBinaryString(k));// 转二进制 10001 System.out.p ...

  10. Flask - 上下文管理(核心)

    参考 http://flask.pocoo.org/docs/1.0/advanced_foreword/#thread-locals-in-flask https://zhuanlan.zhihu. ...