CSU 1553-Good subsequence(RMQ)
2015-12-07 21:55
411 查看
Good subsequence
DescriptionGive you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its maximum value - minimum value<=k. For example n=5, k=2, the sequence ={5, 4, 2,3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.InputThere are several test cases.Each test case contains two line. the first line are two numbers indicates n and k (1<=n<=10,000, 1<=k<=1,000,000,000). The second line give the sequence of n numbers a[i] (1<=i<=n, 1<=a[i]<=1,000,000,000). The input will finish with the end of file.OutputFor each the case, output one integer indicates the answer.Sample Input5 2 5 4 2 3 1 1 1 1Sample Output
3 1
这题就是一个RMQ模板题。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<vector>#include<cmath>using namespace std;#define T 1001000#define inf 0x3f3f3f3f#define mod 1000000007#define lson (rt<<1)#define rson (rt<<1|1)typedef long long ll;int dp_min[T][32],dp_max[T][32];void RMQ_init(const vector<int>& A){int n=A.size();for(int i=0;i<n;++i)dp_max[i][0]=A[i],dp_min[i][0]=A[i];for(int j=1;(1<<j)<=n;++j)for(int i=0;i+(1<<j)-1<n;++i){dp_max[i][j] = max(dp_max[i][j-1],dp_max[i+(1<<(j-1))][j-1]);dp_min[i][j] = min(dp_min[i][j-1],dp_min[i+(1<<(j-1))][j-1]);}}int RMQ_min(int L,int R){int k = 0;while((1<<(k+1))<=R-L+1)k++;return min(dp_min[L][k],dp_min[R-(1<<k)+1][k]);}int RMQ_max(int L,int R){int k = 0;while((1<<(k+1))<=R-L+1)k++;return max(dp_max[L][k],dp_max[R-(1<<k)+1][k]);}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint n,m,i,k;while(~scanf("%d%d",&n,&m)){vector<int> A;for(i=0;i<n;++i){scanf("%d",&k);A.push_back(k);}RMQ_init(A);int c,ma=1;for(i=0;i<n;++i){c = i+1;while(RMQ_max(i,c)-RMQ_min(i,c)<=m&&c<n){c++;ma=max(ma,c-i);}}printf("%d\n",ma);}return 0;}
相关文章推荐
- 通过 poj3368 问题讨论:RMQ问题的 tarjan_lca 求解
- POJ3264-Balanced Lineup RMQ
- [BZOJ2006][NOI2010][RMQ/主席树][二叉堆]超级钢琴
- RMQ问题
- RMQ 模板
- [PKU3264]Balanced Lineup
- Balanced Lineup(RMQ)(POJ 3264)
- poj3264 Balanced Lineup(RMQ +st)
- RMQ Codeforces Round #322 (Div. 2) B. Luxurious Houses
- RMQ_Sparse Table & Segment Tree
- poj_3264 Balanced Lineup 线段树+点修改/RMQ
- RMQ问题
- bzoj-3676 回文串
- codeforces 487B B. Strip(rmq+线段树+二分)
- HDU_4123 && POJ_4003 Bob’s Race (dfs / bfs + RMQ + 尺取)
- POJ_4002 && HDU_4122 Alice's mooncake shop(RMQ)
- UVA 11235 Frequent values(RMQ)
- 一维RMQ和二维RMQ模板以及用法
- zoj 4738 Choir 二维RMQ+预处理
- RMQ uva11235