您的位置:首页 > 产品设计 > UI/UE

hdu3530Subsequence【单调队列优化dp】2010多校联合

2016-02-23 21:57 465 查看
[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
For each test case, print the length of the subsequence on a single line.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

5
4

 

[align=left]Source[/align]
2010 ACM-ICPC Multi-University
Training Contest(10)——Host by HEU
 

低估了多校的难度,高估了自己的实力==从下午4点多弄到现在,勉勉强强写出了了

说题意,给定一个数列,求最大值与最小值大于等于m小于等于k的连续最长长度,想到用单调队列优化最值问题,类似于

FZU - 1894选拔志愿者【单调队列】

的写法,只不过要维护两个数组分别记录递增的最小值和递减的最大值,又如同

hdu3401trade【单调队列优化dp】

我们需要根据已知条件删除队首元素,我们要求最值差不大于k,不小于m,但是队首元素相减只能剪出来最值差的最大值,那索性就用最大差值来控制队首元素呗,那最小差值怎么办?取最优解的时候不用不就完了嘛。还有就是区间长度不是由寻找到的最右边-最左边求出的,而是当前位置减去最左边求出的,最左边一定存在与两个单调队列的队首之一!要不我怎么说他像选拔志愿者那个题呢QAQ队首元素一边用k值控制,控制到满足题意就是最左区间啦

/*****************
hdu3530
2016.2.23
156MS	2900K	1107 B	C++
******************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,k,num[100005],qmin[100005],qmax[100005],dp;
int lmin,rmin,lmax,rmax;
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
//freopen("cin.txt","r",stdin);//单调队列只开一个!!!要不就乱套了
while(~scanf("%d%d%d",&n,&m,&k))
{
lmax=lmin=0,rmax=rmin=-1;
dp=0;
int l=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
while(lmin<=rmin&&num[qmin[rmin]]>num[i]) rmin--;//方向居然能写反!
qmin[++rmin]=i;
while(lmax<=rmax&&num[qmax[rmax]]<num[i]) rmax--;
qmax[++rmax]=i;
while(num[qmax[lmax]]-num[qmin[lmin]]>k)
l=(qmax[lmax]<qmin[lmin])?qmax[lmax++]:qmin[lmin++];
if(num[qmax[lmax]]-num[qmin[lmin]]>=m&&num[qmax[lmax]]-num[qmin[lmin]]<=k)
dp=max(dp,i-l);//我在取值的时候只取了队首,所以不需要控制队尾元素!
}
printf("%d\n",dp);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: