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. 雷赛dmc2410控制卡,驱动器 光栅 加电机

    一般驱动器为9pin接口:(1:VCC 2:限位近端 3:限位远端 4:GND):电源是供给限位器的 ,5:A+ 6:A- 7:B+ 8:B- 注意驱动器的电流大小和细分设置,电流一般1A左右 电流过 ...

  2. static和public的区别

    static:静态.   可以设置:静态类.静态变量.静态方法.   没有使用static修饰的成员为实例成员. 静态成员的使用:通过类名.   1.不加static修饰的成员是对象成员,归每个对象所 ...

  3. 如何保持自己 fork 的项目和原始项目同步

    首先先通过 github 的 web 页面 fork 目标的项目 前提是自己已经设置好了git,并且配置了相应的权限 然后使用git clone命令在本地克隆自己 fork 的项目: git clon ...

  4. Linux下,使用Git管理 dotfiles(配置文件)

    1.管理你的 dotfiles 作为一个计算机深度使用者,并且长期使用 Linux 作为主要操作系统,折腾各种功能强大的软件是常有的事儿.这些软件有它们各自的配置文件,通常以 . 开头,因此有人管它们 ...

  5. CF721C. Journey

    传送门 说实话,这是一道非常简单的DP题,简单到如果放到NOIp第二题可能都有些差强人意,然而我写崩了. 所以简单记录一下. 需要注意的是,这道题的DP应该是从$N$点开始,以1为边界,满足最短路的三 ...

  6. php 简单分页类

    /**  file: page.class.php   完美分页类 Page  */ class Page {  private $total;          //数据表中总记录数  privat ...

  7. Python获取文件名

    本文实例讲述了python实现从URL地址提取文件名的方法.分享给大家供大家参考.具体分析如下: 如:地址为 http://www.jb51.net/images/logo.gif 要想从该地址提取l ...

  8. Zipf定律

    http://www.360doc.com/content/10/0811/00/84590_45147637.shtml 英美在互联网具有绝对霸权 Zipf定律是美国学者G.K.齐普夫提出的.可以表 ...

  9. nginx+php-fpm的socket配置小结

    关于socket的介绍本文不再赘述,生产环境中常用socket方式,本文简述其配置方式. #cd /app/local/php#切换到php安装目录下 #mkdir run #chmod 777 ./ ...

  10. typedef和#define的用法与区别

    typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程 ...