hdu3530 Subsequence

Problem Description

There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.

Input

There are multiple test cases.

For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range [1, 100000]. m and k are in the range [0, 1000000]. The second line has n integers, which are all in the range [0, 1000000].

Proceed to the end of file.

Output

For each test case, print the length of the subsequence on a single line.

Sample Input

5 0 0
1 1 1 1 1
5 0 3
1 2 3 4 5

Sample Output

5

4

这题我用的是rmp算法,先初始化2的次方的区间最大值最小值,然后循环算出最大的区间长度。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int a[100006];
int minx[100100][30];
int maxx[100100][30];
void init_rmq(int n)
{
	int i,j;
	for(i=1;i<=n;i++)maxx[i][0]=minx[i][0]=a[i];
	for(j=1;j<=20;j++){
		for(i=1;i<=n;i++){
			if(i+(1<<j)-1<=n)
			{maxx[i][j]=max(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]);
			minx[i][j]=min(minx[i][j-1],minx[i+(1<<(j-1))][j-1]);}
		}
	}
}

int getmax(int l,int r)
{
	int k,i;
	if(l>r)swap(l,r);
	k=(log((r-l+1)*1.0)/log(2.0));
	return max(maxx[l][k],maxx[r-(1<<k)+1][k]);
}

int getmin(int l,int r)
{
	int k,i;
	if(l>r)swap(l,r);
	k=(log((r-l+1)*1.0)/log(2.0));
	return min(minx[l][k],minx[r-(1<<k)+1][k]);
}

int main()
{
	int n,m,i,j,k,l,r,ans,t;
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		init_rmq(n);
		l=1;ans=0;           //l不用每次从1开始判,因为之前的l越小,r-l+1的值就越大。
		for(i=1;i<=n;i++){
			r=i;
			if(l>r)continue;
			while(getmax(l,r)-getmin(l,r)>k)l++; //如果大于k,那么后面的r对于此时的l肯定不满足不大于k,所以必须l++;
			if(getmax(l,r)-getmin(l,r)>=m && getmax(l,r)-getmin(l,r)<=k)ans=max(ans,r-l+1);
		}
		printf("%d\n",ans);
	}
	return 0;
}

时间: 2024-10-30 08:42:40

hdu3530 Subsequence的相关文章

hdu3530 Subsequence 单调队列

// hdu3530 Subsequence 单调队列 // 题目大意:找到一个最大的子串,使得子串区间内最大值和最小值的差 // 在low和up范围内,串的规模10w. // 解题思路: // 单调队列,单调队列可以保留i位置之前的最大值和最小值的下标,有了这些 // 则,每次我们比较两个队列的队头,看差值是否大于up,(因为它是到i位置最大 // 的差值,其他值不可能比i还要大.) // 如果大于,则将两个对头靠前的那个丢掉,即出队,再比较对头,并且记录下 // 出队的位置下标,(因为此时出

HDU3530 Subsequence(单调队列)

题意是说给出一个序列,现在要求出这个序列的一个最长子区间,要求子区间的最大值与最小值的差在[m, k]范围内,求区间长度 做法是维护两个队列,一个维护到当前位置的最大值,一个维护最小值,然后计算当前节点i作为右端点的最常区间长度,那么扫描两个队列,维持单调性. 然后比较两个队列头的差值, 1.如果差值满足条件,那么记录答案: 2.如果差值小于m,那么此时没有答案,说明没有以i作为右端点的区间满足条件(表示前i个数的最大值减去前i个数的最小值的差<m,那么不论如何调整起点,都不可能有解) 3.如果

单调队列总结

单调队列 就是保持队列中的元素始终保持单调性,这个数据结构就是单调队列 它的作用就是维护最值.求第一个比i小(大)的数的下标等等 还有个单调栈来着,不过我们可以用一个双端队列就足够了 如果要维护最大值,就用单调递减队列,反之,用递增队列 1.hdu3530 Subsequence 单调队列入门题 这题求的是最大值减去最小值不小于m不大于k的最长长度 这题用单调队列维护最大值和最小值即可 #include<iostream> using namespace std; #define MAX 10

单调队列————[USACO09MAR]向右看齐Look Up

先了解一下单调队列: 很明显的具有单调性 分为单调递增和单调递减两种,简单点讲就是维护队头为最大值或者为最小值 (建议采用双向队列  比较好写) 具体步骤:(这个是单调递减) 如果队列非空且当前值比队尾元素大,不断删除比该值小的元素,否则直接队尾入队 while(!que.empty()&&ma[i]>que.back()) {     que.pop_back(); } que.push_back(i); 单调队列的作用:: 1):可以用来维护区间的单调性,用来解决最大或最小的问题

Subsequence(hdu3530)

Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6141    Accepted Submission(s): 2041 Problem Description There is a sequence of integers. Your task is to find the longest subsequence

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

ZOJ3349——Special Subsequence

Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n integers , and A is a special subsequence thatsatisfies |Ai-Ai-1| <= d ( 0 <i<=|A|)) Now your task is to find the longest special subsequence of a cer