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. Steam 游戏 《The Vagrant(流浪者)》修改器制作-[先使用CE写,之后有时间的话改用CheatMaker](2020年寒假小目标08)

    日期:2020.02.07 博客期:146 星期五 [温馨提示]: 只是想要修改器的网友,可以直接点击此链接下载: 只是想拿CT文件的网友,可以直接点击此链接下载: 没有博客园账号的网友,可以将页面下 ...

  2. 【转】bug management process

    What is Bug? A bug is the consequence/outcome of a coding fault What is Defect? A defect is a variat ...

  3. SpringBoot 获得 properties 文件中数据方式

    参考:https://blog.csdn.net/qq_37171353/article/details/78005845

  4. 02-04Android学习进度报告四

    今天主要学习Android界面的构建,包括Textview.EdixtText.Button等元素的应用. 关于Textview,主要是以下属性: id:为TextView设置一个组件id,根据id, ...

  5. 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制

    原文:http://blog.csdn.net/jason0539/article/details/18075293 这算是如何在两个activity之间传递handler的解决方案二了,解决方案一见 ...

  6. Linux centosVMware MySQL常用操作设置更改root密码、连接mysql、mysql常用命令

    一.设置更改root密码 启动mysql /usr/local/mysql/bin/mysql -uroot 更改环境变量PATH,增加mysql绝对路径 使mysql -uroot永久生效需要编辑, ...

  7. DRF项目之层级关系

    一共分为四层关系. 第一层:视图 用来接收前端传递的参数,并实现业务逻辑相关的数据处理,并将数据通过创建序列化器对象的形式传递给序列化器. 第二层:序列化器 接收前端传递的数据,并将数据进行序列化操作 ...

  8. Golang mysql数据库

    基本操作: Open() – create a DB Close() - close the DB Query() - 查询 QueryRow() -查询行 Exec() -执行操作,update,i ...

  9. 5.1 Nginx的基本配置

    备注:worker_processes 1(数量建议跟系统CPU的核数相同,例如:2个CPU,每个CPU4核,建议为8),worker_connections 建议小于worker_rlimit_no ...

  10. Redis第一篇章 Hello Word!

    1.找到redis目录 2.在新建一个文件夹(myredis) 3.将redis.conf 进行复制到myredis 文件夹里面 4.启动redis redis-server /home/dc2-us ...