RMQ+二分。。。。
枚举 i  ,找比 i 小的第一个元素,再找之间的第一个最大元素。。。。。

                  Sticks Problem
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 9338   Accepted: 2443

Description

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

Source

 
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int dp_max[][],dp_min[][],a[],n; int _max(int l,int r)
{
if(a[l]>=a[r]) return l;
else return r;
} int _min(int l,int r)
{
if(a[l]<=a[r]) return l;
else return r;
} void Init()
{
for(int i=;i<=n;i++)
dp_max[i][]=dp_min[i][]=i;
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<=n;i++)
{
int m=i+(<<(j-));
dp_max[i][j]=_max(dp_max[i][j-],dp_max[m][j-]);
dp_min[i][j]=_min(dp_min[i][j-],dp_min[m][j-]);
}
}
} int rmq_min(int L,int R)
{
int k=;
while(L+(<<k)-<=R) k++; k--;
return _min(dp_min[L][k],dp_min[R-(<<k)+][k]);
} int rmq_max(int L,int R)
{
int k=;
while(L+(<<k)-<=R) k++; k--;
return _max(dp_max[L][k],dp_max[R-(<<k)+][k]);
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",a+i);
Init();
int ans=,r,k;
for(int i=;i+ans<n;i++)
{
int low=i+,high=n,mid;
while(low<=high)
{
if(low==high) break;
mid=(low+high)>>;
if(a[i]<a[rmq_min(low,mid)])
low=mid+;
else
high=mid;
}
r=low;
k=rmq_max(i,r);
if(a[i]<a[k])
ans=max(ans,k-i);
}
if(ans==) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}

POJ 2452 Sticks Problem的更多相关文章

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

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

  2. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  3. POJ 1681---Painter's Problem(高斯消元)

    POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...

  4. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  5. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...

  6. Sticks Problem

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

  7. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

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

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

  9. Day6 - I - Sticks Problem POJ - 2452

    Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented b ...

随机推荐

  1. Maven异常:Could not find artifact

    用Maven build("clean tomcat7:run" )  Maven聚合工程时,出现了一下问题: [INFO] Scanning for projects... [E ...

  2. Fluent interface

    In software engineering, a fluent interface (as first coined by Eric Evans and Martin Fowler) is an ...

  3. java中Scanner和random的用法

    Scanner是默认按照行来读取数字的. 创建一个用来输入的函数 Scanner scan=new Scanner(System.in):system.in是表示从控制台输入. 然后用一个变量类接收这 ...

  4. 嵌入式Linux系统开发环境搭建

    Linux kernel Complier: http://supportopensource.iteye.com/blog/680483 sudo make mrproper         净化解 ...

  5. Repeater 获取数据值

    <input id="btn_fld_PRD_UM" class="btn" type="button" value="选择 ...

  6. C#读取Excel,DataTable取值为空的解决办法

    连接字符串这么些就行了 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + opnFileName ...

  7. Android学习笔记——LinearLayout

    该工程的功能是实现LinearLayout 以下的代码是MainActivity.java中的代码 package com.example.linearlayout; import android.a ...

  8. Red Black Tree in C

    http://web.mit.edu/~emin/www.old/source_code/red_black_tree/index.html

  9. yourphp读取不到hits

    源代码 <YP:list name="Article" order="id desc" catid="37" limit=" ...

  10. 快捷键_Mac

    苹果Mac系统常用快捷键 Command+Tab 任意情况下切换应用程序 - 向前循环 Shift+Command+Tab 切换应用程序 - 向后循环 Command+L 当前程序是浏览器时,可以直接 ...