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

  1. 4
  2. 5 4 3 6
  3. 4
  4. 6 5 4 3

Sample Output

  1. 1
  2. -1
  3.  
  4. 思路:求每个点往右的最大值且都不小于他,先想到了单调队列,区间最值就用ST表,整了整发现WA了,发现并不需要两端队列,遍历每一个点找右侧就行了,好像叫单调栈,找到比每个点小的第一个点,在这个区间内找最大点,用二分加速寻找就行了
  1. const int maxm = 5e4+;
  2.  
  3. int Max[maxm][], Min[maxm][], a[maxm], N;
  4.  
  5. void init() {
  6. memset(Max, , sizeof(Max)), memset(Min, , sizeof(Min));
  7. }
  8.  
  9. int BisearchMin(int pos) {
  10. int l = pos+, r = N, mid, ans = N, k, t;
  11. while(l <= r) {
  12. mid = (l + r) >> ;
  13. k = log((double)(mid-pos+)) / log(2.0);
  14. t = min(Min[pos][k], Min[mid - ( << k) + ][k]);
  15. if(t < a[pos]) {
  16. ans = mid;
  17. r = mid - ;
  18. } else
  19. l = mid + ;
  20. }
  21. return ans;
  22. }
  23.  
  24. int BisearchMax(int l, int r) {
  25. int k, ans, mid, t, t2;
  26. k = log((double)(r - l + )) / log(2.0);
  27. t = max(Max[l][k], Max[r - (<<k)+][k]);
  28. while(l <= r) {
  29. mid = (l + r) >> ;
  30. k = log((double)(mid - l + )) / log(2.0);
  31. t2 = max(Max[l][k], Max[mid - (<<k)+][k]);
  32. if(t2 == t) {
  33. ans = mid;
  34. r = mid - ;
  35. } else
  36. l = mid + ;
  37. }
  38. return ans;
  39. }
  40.  
  41. int main() {
  42. while(scanf("%d", &N) != EOF) {
  43. init();
  44. int ans = -;
  45. for(int i = ; i <= N; ++i) {
  46. scanf("%d", &a[i]);
  47. Max[i][] = Min[i][] = a[i];
  48. }
  49. for(int k = ; (<<k) <= N; ++k) {
  50. for(int i = ; i+(<<k)- <= N; ++i) {
  51. Max[i][k] = max(Max[i][k-], Max[i+(<<(k-))][k-]);
  52. Min[i][k] = min(Min[i][k-], Min[i+(<<(k-))][k-]);
  53. }
  54. }
  55. for(int i = ; i <= N; ++i) {
  56. int r = BisearchMin(i);
  57. r = BisearchMax(i, r);
  58. if(i != r)
  59. ans = max(ans, r - i);
  60. }
  61. printf("%d\n", ans);
  62. }
  63. return ;
  64. }
  1.  

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. android 支持上拉加载,下拉刷新的列表控件SwipeRefreshLayout的二次封装

    上拉加载,下拉刷新的列表控件,大家一定都封装过,或者使用过 源代码,我会在最后贴出来 这篇代码主要是为了解决两个问题 1.滑动冲突得问题 2.listview无数据时,无数据布局的展示问题 下方列出的 ...

  2. Tomcat认识

    Tomcat目录结构: bin:存放启动和关闭的一些脚本 common:共享(部署在该服务器上的一些)jar包 conf:存放服务器的一些配置文件 webapps:部署文件 work:服务器运行时,产 ...

  3. ➡️➡️➡️leetcode 需要每天打卡,养成习惯

    目录 待完成的 完成的 0204 0203 以前 java 的 ! 的操作 不像 c 那样自由,!不要使用在int 变量上 c ^ 是异或操作 体会:c中,malloc 后的新建的数组,默认不是0(j ...

  4. LeetCode中等题(二)

    题目一: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  5. 我的学习经历——Linux系统入门教程

    我想把最近学习Linux的经验和过程分析出来,当时是在上大三,是学生一枚,以前对开源也没有什么特殊的认识,只觉得很高深,不明觉厉的东西,在当时因为学校要参加职业技能大赛,其中有一团体性质的比赛,几个同 ...

  6. LSTM算法公式

    参考:<基于强化学习的开放领域聊天机器人对话生成算法>

  7. ApacheDbUtilsTest

    ApacheDbUtilsTest package p1; import com.DataSourceUtil; import entity.Student; import org.apache.co ...

  8. 32 commons-lang包学习

    maven依赖 <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lan ...

  9. redhat 7.6 rpm ,yum ,编译安装

    rpm rpm  -ivh  包名  //安装 rpm  -e     包名   //卸载 which mount  查看命令安装目录 rpm  -qf    /usr/bin/mount    // ...

  10. PyCharm底部控制台console界面开启/取消自动换行

    File --> Settings --> Editor --> General --> Console中 勾选右侧第一项Use soft wraps in console(选 ...