同 HDU 2836 只不过改成了求最长子串。
DP+线段树单点修改+区间查最值。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; const int MAXN = ; int n, d;
int val[MAXN];
int num[MAXN];
int dp[MAXN]; int maxi[ MAXN << ]; void pushUp( int rt )
{
maxi[rt] = max( maxi[rt << ], maxi[rt << | ] );
return;
} void Update( int L, int c, int l, int r, int rt )
{
if ( l == L && L == r )
{
maxi[rt] = c;
return;
} int m = ( l + r ) >> ;
if ( L <= m ) Update( L, c, lson );
else Update( L, c, rson );
pushUp( rt );
return;
} int Query( int L, int R, int l, int r, int rt )
{
if ( L <= l && r <= R )
return maxi[rt]; int m = ( l + r ) >> ; int res = ;
if ( L <= m ) res = max( res, Query( L, R, lson ) );
if ( R > m ) res = max( res, Query( L, R, rson ) ); return res;
} int main()
{
while ( ~scanf( "%d%d", &n, &d ) )
{
for ( int i = ; i <= n; ++i )
{
scanf( "%d", &val[i] );
num[i] = val[i];
} sort( num + , num + + n );
int cnt = unique( num + , num + n + ) - num - ; dp[] = ;
memset( maxi, , sizeof(maxi) );
int ans = ; for ( int i = ; i <= n; ++i )
{
int id = lower_bound( num + , num + cnt + , val[i] ) - num;
int left = lower_bound( num + , num + cnt + , val[i] - d ) - num;
int right = upper_bound( num + , num + cnt + , val[i] + d ) - num - ;
dp[i] = Query( left, right, , n, ) + ;
ans = max( ans, dp[i] );
Update( id, dp[i], , n, );
}
printf( "%d\n", ans );
}
return ;
}