明知道是有条件的LIS,但条件怎么也写不对。。。看了别人的,居然用个数组存储前驱下标,是在下输了。。。
还有注意点,写注释里了。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100005;
const int INF = 1000000;
int ans, a[N], dp[N], pos[N];
int n, d;
int LIS()
{
int i,j;
ans = 0;
for(i = 1; i <= n; i++)
{
pos[i] = lower_bound(dp + 1, dp + n + 1, a[i]) - dp;//返回插入下标值
ans = max(ans, pos[i]);//有效下标最大长度
j = i - d;//前驱下标
if(j > 0) dp[pos[j]] = min(dp[pos[j]], a[j]);//更新最小有效值,由于二分查找到的下标是当前下标,所以调用前驱节点时不一定比最大值小,必须取一次最小值
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int i;
while(~scanf("%d%d", &n, &d))
{
for(i = 1; i <= n; i ++)
{
scanf("%d",&a[i]);
dp[i] = INF;
}
printf("%d\n",LIS());
}
return 0;
}