标签:des style blog class code java
5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
5 4
题意:求一个最长子序列,使得最大值与最小值差值在mi,mx范围内,
用一个递增,一个递减两个单调队列维护最大值,最小值,然后扫描一遍,队列的起点从最近一个被单调队列抛弃的下标+1算起。
代码:
/* *********************************************** Author :_rabbit Created Time :2014/5/13 10:30:48 File Name :C.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <string> #include <time.h> #include <math.h> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; int a[100100],que1[100100],que2[100100]; int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,mx,mi; while(~scanf("%d%d%d",&n,&mi,&mx)){ for(int i=1;i<=n;i++)scanf("%d",&a[i]); int head1=0,tail1=0,head2=0,tail2=0; int last1=0,last2=0; int ans=0; for(int i=1;i<=n;i++){ while(head1<tail1&&a[que1[tail1-1]]>a[i])tail1--;//递增 que1[tail1++]=i; while(head2<tail2&&a[que2[tail2-1]]<a[i])tail2--;//递减 que2[tail2++]=i; while(a[que2[head2]]-a[que1[head1]]>mx){ if(que1[head1]<que2[head2])head1++; else head2++; } if(a[que2[head2]]-a[que1[head1]]>=mi) ans=max(ans,i-max(que1[head1-1],que2[head2-1])); } cout<<ans<<endl; } return 0; }
标签:des style blog class code java
原文地址:http://blog.csdn.net/xianxingwuguan1/article/details/25739413