题目链接:点击打开链接
题目大意:找LIS其中有0的话,0可以代替任何数。
解题思路:找到每个0之前的LIS,0作为前一段LIS的最后一位+1,前面的序列尽可能的小,于是加上一个剪枝操作,当前面的序列出现0~n,就可以剪掉了。
代码:
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<ctime>
#include "cstdio"
#include "string"
#include "string.h"
#include "map"
#include "bitset"
using namespace std;
const int INF = 0x3f3f3f3f;
int a[100001];
int main()
{
int T, cas = 0;
scanf("%d", &T);
while(T--)
{
int n, x, ans = 0, pos;
scanf("%d", &n);
memset(a, INF, sizeof(a));
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
if (x)
{
pos = lower_bound(a, a + n, x) - a;
a[pos] = x;
}
else
{
pos = lower_bound(a, a + n, INF) - a;
for (int j = pos - 1; j >= 0; j--)
{
a[j + 1] = a[j] + 1;
if (a[j] == j)
break;
}
a[0] = 0;
}
ans = max(ans, pos+1);
}
printf("Case #%d: %d\n", ++cas, ans);
}
return 0;
}