Sticks Problem--(单调队列)
来源:互联网 发布:泉州用友软件 编辑:程序博客网 时间:2024/05/16 04:40
Sticks Problem
Time Limit : 12000/6000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 64 Accepted Submission(s) : 19
Problem 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.
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. <br>Line 1: a single integer n (n <= 50000), indicating the number of sticks. <br>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
45 4 3 646 5 4 3
Sample Output
1-1
i和j之间数k(i<k<=j)满足2个条件:1.a[i]<a[k], 2.a[k]<=a[j]; ,很搞笑对吗,后面这两个条件是分别证的
代码:
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long int ll;int n;int a[50005],q[50005],r[50005];int main(){ int i,j; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&a[i]); int tail=1; q[1]=n+1; for(i=n;i>=1;i--) //用单调队列求出a[i]右边连续大于他的数有几个 { while(tail>=1&&a[q[tail]]>a[i]) tail--; r[i]=q[tail]-1-i; q[++tail]=i; } int maxn,temp,ans=0; for(i=1;i<=n;i+=temp+1) { maxn=temp=-1; for(j=0;j<=r[i];j++) //对于大于他的这些数,都是满足条件1的 { if(a[i+j]>maxn) //只要a[i+j]大于maxn,说明他是目前最大的也就是以他结尾的话前面a[i+1]至a[i+j]都满足条件2 { maxn=a[i+j]; temp=j; //此时记录下j,下面i+=temp+1;就从那些没算到的开始 } } if(ans<temp) ans=temp; } if(ans!=0) printf("%d\n",ans); else printf("-1\n"); } return 0;}
阅读全文
0 0
- Sticks Problem (单调队列)
- Sticks Problem--(单调队列)
- FZU problem 1896 单调队列
- A-B Problem(Dec)解题报告 - 单调队列
- hdu 3706 Second My Problem First(单调队列)
- hdu 3706 Second My Problem First(单调队列)
- POJ2452---Sticks Problem(单调栈+RMQ,方法不够优秀)
- hdu2993 MAX Average Problem(斜率+单调队列)
- foj Problem 1894 志愿者选拔---单调队列
- fzoj Problem 1894 志愿者选拔 【单调队列】
- FZU Problem 1894 志愿者选拔 单调队列
- HDU3706Second My Problem First(单调队列)
- 【单调队列】HDU_3706_Second My Problem First
- poj2823(单调队列队列)
- 单调队列 (POJ2823)
- poj2823(单调队列)
- poj2823(单调队列)
- ssoj1589Pilots(单调队列)
- BZOJ 2242 BSGS
- 多线程~~简单的线程创建,C语言实现
- 透彻理解迪杰斯特拉算法
- 移动端页面开发的两种方式
- Kotlin 类和继承
- Sticks Problem--(单调队列)
- MacOs下phpstorm Xdebug配置超详细
- SSL2689 2017年8月14日提高组T3 染色(树形dp)
- 网络流——最大流增广路算法
- shell编程 if语句
- COGS-2258 复仇的序幕曲(动态树分治)
- 计算机网络环境的搭建
- PTA-自测-4 Have Fun with Numbers
- Primer (四)