Question: ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.
题目大意:求给定的数组的LIS,其中0可以变为任何数。
题解:对于求LIS的问题,我们有一个O(nlogn)的方法。维护一个单增序列,其第i位代表当前前k位中长度为i的LIS中最小的末位是多少。如果碰到0,则令每个当前串的长度加一,且最后一位加1。做一遍即可知道答案。
#include <cstdio>
#include <iostreaM>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int n,a[maxn];
int dp[maxn];
int cnt = 0;
int main()
{
freopen("10.in","r",stdin);
freopen("10.out","w",stdout);
int kase;scanf("%d",&kase);
for(int t = 1;t<=kase;t++){
cnt = 0;dp[0]=-1;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
if(a[i]!=0) {
int p = lower_bound(dp,dp+cnt+1,a[i]) - dp;
if(p>cnt) cnt++;
dp[p]=a[i];
}
else {
cnt++;
for(int i=cnt;i>=1;i--) {
dp[i]=dp[i-1]+1;
}
}
}
printf("Case #%d: %d\n",t,cnt);
}
return 0;
}