题目:
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5146 Accepted Submission(s): 1712
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.
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
Source
Recommend
zhengfeng
题意:给一个长度为n的序列,求出其中最长的子序列,满足最大值与最小值之差大于等于m小于等于k。
思路:可以用一个单调队列来维护序列的最大值和最小值,如果最大值与最小值之差大于k,那么就让最大值队列的队首或最小值队列的队首中位置较小的那个出队,直到满足最大值与最小值之差小于等于k,选位置较小的出队是因为要使序列的长度最长,出队后的新区间的起点为出队的位置+1.然后再判断最大值与最小值之差是否满足大于等于m,
如果小于m不必出队,因为出队不可能使它们的差变大,如果大于等于m再考虑更新ans。
代码:
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}
/*#ifdef HOME
freopen("in.txt","r",stdin);
#endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME
int Scan()
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
int a[100000+5];
struct node
{
int num;
int po;
};
node q1[100000+5],q2[100000+5];
int n,m,k;
int main()
{while(RIII(n,m,k)!=EOF)
{for(int i=0;i<n;i++)
RI(a[i]);
int front1=0,rear1=0,front2=0,rear2=0;
int ans=0;
int now=0;
for(int i=0;i<n;i++)
{
while(front1<rear1&&q1[rear1-1].num>a[i])
rear1--;
q1[rear1].num=a[i];
q1[rear1++].po=i;
while(front2<rear2&&q2[rear2-1].num<a[i])
rear2--;
q2[rear2].num=a[i];
q2[rear2++].po=i;
while(front1<rear1&&front2<rear2&&q2[front2].num-q1[front1].num>k)
{
if(q1[front1].po<=q2[front2].po)
{ now=q1[front1].po+1;
front1++;
}
else
{
now=q2[front2].po+1;
front2++;
}
}
if(front1<rear1&&front2<rear2&&q2[front2].num-q1[front1].num>=m)
ans=max(ans,i-now+1);
}
printf("%d\n",ans);
}
return 0;
}