【HDU】3530 Subsequence子序列
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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
翻译
子序列
时间限制:2000/1000 MS(Java /其他)
内存限制:65536/32768 K(Java /其他)
问题描述
有一个整数序列。您的任务是找到满足以下条件的最长子序列:子序列的最大元素和最小元素之间的差不小于m且不大于k。
输入
有多个测试用例。
对于每个测试用例,第一行都有三个整数n,m和k。n是序列的长度,并且在[1,100000]范围内。m和k在[0,1000000]范围内。第二行具有n个整数,均在[0,1000000]范围内。
继续到文件末尾。
输出
对于每个测试用例,在一行上打印子序列的长度。
样本输入
5 0 0
1 1 1 1 1
5 0 3
1 2 3 4 5
样本输出
5
4
思路
用单调最小值和单调最大值做
枚举右端点
若差>k
记录和删除离当前点最远的点
若差<m
跳过
答案=max(当前点-记录值)
代码
#include<iostream>
#include<cstdio>
#include<deque>
using namespace std;
int a[100010];
deque<int>tem;
deque<int>mn;
deque<int>mx;
int main()
{
int n,ans,i,l,r,temp;
while(cin>>n>>l>>r)
{
mn=mx=tem;
ans=temp=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(;!mx.empty()&&a[i]>=a[mx.back()];mx.pop_back());//删除小于a[i]的数
mx.push_back(i);
for(;!mn.empty()&&a[i]<=a[mn.back()];mn.pop_back());//删除大于a[i]的数
mn.push_back(i);
while(!mx.empty()&&!mn.empty()&&a[mx.front()]-a[mn.front()]>r)//找不在范围内数
if(mx.front()<mn.front()) temp=mx.front(),mx.pop_front();
else temp=mn.front(),mn.pop_front();
if(!mx.empty()&&!mn.empty()&&a[mx.front()]-a[mn.front()]>=l)ans=max(ans,i-temp);
}
printf("%d\n",ans);
}
return 0;
}