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. lua中的table、stack和registery

    ok,前面准备给一个dll写wrapper,写了篇日志,看似写的比较明白了,但是其实有很多米有弄明白的.比如PIL中使用的element,key,tname,field这些,还是比较容易混淆的.今天正 ...

  2. ASP.NET MVC 给ViewBag赋值Html格式字符串的显示问题总结

    今天再给自己总结一下,关于ViewBag赋值Html格式值,但是在web页显示不正常; 例如,ViewBag.Content = "<p>你好,我现在测试一个东西.</p& ...

  3. 【Alpha版本】 第六天 11.14

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成管理员的三大界面框架.完成管理主界面 完成我要招聘的招聘详情显 ...

  4. Base64复习

    http://www.cnblogs.com/chengxiaohui/articles/3951129.html

  5. DNS介绍

    DNS出现及演化 网络出现的早期 是使用IP地址通讯的,那时就几台主机通讯.但是随着接入网络主机的增多,这种数字标识的地址非常不便于记忆,UNIX上就出现了建立一个叫做hosts的文件(Linux和w ...

  6. glade2支持C++代码的输出(3)

    今天完成了glade-2生成configure.ac/Makefile.am等调整 代码为:cpp_out_4.patch.zip BaseObject类也做了一些小的调整:BaseObject.00 ...

  7. redis哨兵配置

    redis哨兵配置主从   redis哨兵的启动和redis实例的启动没有关系.所以可以在任何机器上启动redis哨兵.至少要保证有两个哨兵在运行,要不然宕机后哨兵会找不到主节点. 配置步骤: 1.在 ...

  8. UDP 网络通信 C#

    接收端   using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thre ...

  9. Jasper(物联网网络支撑平台公司)的技术为什么这么牛逼?

    Jasper在这个行业积累了十几年,合作的运营商超过30个,合作的行业大咖包括了通用.空客.宝马.特斯拉等几千个行业龙头,还是有很多积累下来的优势的. 一是,Jasper通过积累下来的行业应用经验,针 ...

  10. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...