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. 通过oauth 认证 新浪微博 开发过程中遇到的问题

    1 Android之NetworkOnMainThreadException异常 http://blog.csdn.net/mad1989/article/details/25964495 ,从Hon ...

  2. MYSQLDUMP参数详解

    mysqldump客户端可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器).转储包含创建表和/或装载表的SQL语句. 如果你在服务器上进行备份,并且表 ...

  3. Android应用程序消息处理机制

    http://download.csdn.net/detail/luoshengyang/6439647 pdf

  4. 2 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之MySql的搭建

    preface 上一节我们讲了DRBD的原理,以及如何部署DRBD,那么现在在上一节的基础上部署Mysql 安装并启动Mysql 为了方便,我一般采用yum安装Mysql.命令如下: 在172.16. ...

  5. linux系统命令

    TOP命令 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程, 即可以通过用户按键来不断刷新当前状态.如 ...

  6. D/A转换器实验

    1.代码: #include<reg52.h>typedef unsigned char u8;typedef unsigned int u16;void delay (u16 num){ ...

  7. css014 响应式web设计

    css014 响应式web设计 一.    响应式web设计基础知识 1.rwd的三大理念:a.用于布局的弹性网络, b.用于图片和视频的弹性媒体,c.为不同屏幕宽度创建的不同样式的css媒体查询. ...

  8. css让图片作为按钮的背景并且大小合适

    最近在做ASP大作业,在做html页面的时候想把一个图片作为按钮的背景,搞了好久终于在csdn上找到了满意的答案: background-size: cover; 只需要这一句就ok了,就是这么简答. ...

  9. Java——Swing事件处理

    import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JFrame; ...

  10. RandomAccessFile类

    File类只是针对文件本身进行操作,而如果要对文件内容进行操作,则可以使用RandomAccessFile类,此类属于随机读取类,可以随机地读取一个文件中指定位置的数据. //============ ...