您的位置:首页 > 其它

LIS(变形) HDOJ 5489 Removed Interval

2015-09-29 16:24 302 查看
题目传送门

题意:求删掉连续L长度后的LIS

分析:记rdp[i]表示以a[i]为开始的LIS长度,用nlogn的办法,二分查找-a[i]。dp[i]表示以a[i]为结尾并且删去[i-L-1, i-1]的LIS,ans = max (dp[i] + rdp[i] - 1),还要特别考虑删去最后L的长度

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/29 星期二 15:11:29
* File Name     :F.cpp
************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
int a
;
int dp
, rdp
;
int d
;

int main(void)    {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
int n, L;  scanf ("%d%d", &n, &L);
for (int i=1; i<=n; ++i)    {
scanf ("%d", &a[i]);
}

int ans = 0;
memset (d, INF, sizeof (d));
for (int i=n; i>=1; --i)    {
int k = lower_bound (d+1, d+1+n, -a[i]) - d;
d[k] = -a[i];
rdp[i] = k;                                                                 //以a[i]为开始的LIS长度
}
memset (d, INF, sizeof (d));
for (int i=1; i<=n; ++i)    {
if (i - L - 1 >= 1) d[lower_bound (d+1, d+1+n, a[i-L-1]) - d] = a[i-L-1];
dp[i] = lower_bound (d+1, d+1+n, a[i]) - d;                                 //以a[i]为结尾的LIS长度
if (i > L) ans = max (ans, dp[i] + rdp[i] - 1);
}
if (n > L) ans = max (ans, (int) (lower_bound (d+1, d+1+n, a[n-L]) - d));       //删去最后的L长度
printf ("Case #%d: %d\n", ++cas, ans);
}

return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: