Problem Description
?? 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.
Input
The first line contains an interger T,denoting the number of the test cases.(T <= 10)
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.
Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.
Sample Input
2 7 2 0 2 1 2 0 5 6 1 2 3 3 0 0
Sample Output
Case #1: 5 Case #2: 5HintIn the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
我们可以发现,对于最大的ans,必定有一种情况可以做到取了所有的0
比如4 0 5这种情况。我们可以把0变成5而放弃后面一个5
这样的话就可以去掉0,在总答案上加上0的总数
可是题目要求严格单调增,所以我们需要把后面的数变成不能转移的状态
所以每个数减去前面0的个数,然后直接跑LIS即可
【考场上突然不会写LIS了...下面是队友写的程序】
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100010;
int num[N];
int main(void){
int t,ti;
int n;
int i,j,k;
int r,p;
int cas = 1;
int cnt;
scanf("%d",&t);
for(ti=1;ti<=t;ti++){
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&r);
num[i] = r;
}
cnt = 0;
for(i=1,j=1;i<=n;i++){
if(num[i]==0){
cnt++;
}
else{
num[i] = num[i]-cnt;
num[j++] = num[i];
}
}
n = n-cnt;
int len = 1;
if(n==0){
len = 0;
}
for(i=2;i<=n;i++){
int l = 0,r = len;
while(l<r){
int mid = (l+r+1)/2;
if(num[mid]<num[i]){
l = mid;
}
else{
r = mid - 1;
}
}
if(l==len){
len = l+1;
}
num[l+1] = num[i];
}
printf("Case #%d: %d\n",ti,len+cnt);
}
return 0;
}