Description?? Gets an sequence S with n intergers (0 < n <= 100000,0<= S[i] <= 1000000).?? Have a magic so, he can change 0 to any interger (he does not need to change all 0 to the same interger).?? Wants him to find out the length of the longest increasing (strictly) subsequence he can get.InputThe first line contains an Interger t,denoting the number of the the test cases. (T <= 10)
For each case,the first line contains a Interger N,which is the length of the array s.
The next line contains n Intergers separated is a single space, and denote each number in S.OutputFor 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
272 0 2 1 2 0 561 2 3 3 0 0
Sample Output
Case #1:5Case #2:5
HintIn the first case,you can change the second 0 to 3.So of the longest increasing subsequence is 0 1 2 3 5.
English topics difficult to read, but test instructions simple, is to give a sequence of integers, 0 can become any number, to find out the length of his longest ascending sub-sequence, must be an example, 2 0 5 of 0 into 3, the oldest sequence is 0 1 2 3 5.
for this problem can not directly find the length of the longest ascending sub-sequence, it is possible to let each number minus the number of its front 0, and then find out the length of the longest ascending sub-sequence of a number of non-0, and finally find the length plus zero number.
Suppose that all 0 are in the longest continuous subsequence, for example a 0 0 0 B, can not look at B, a 0 0 0 as a continuous ascending sequence, with B minus 0 of the number of b-3, if b-3>a a can be greater than the last zero value, a b-3 is not 0 The oldest sequence, the length of 2, Plus the number of 0, the final result is 5, if the value of the B-3<a, the oldest sequence can only go to the last 0, a or B is the oldest in the non-zero sequence, the length of 1, plus the number of 0, the final result is 4, the final sequence is a 0 0 0 (0 is any number)
1#include <cstdio>2#include <algorithm>3 #defineINF 0x3f3f3f3f4 using namespacestd;5 intb[100100],g[100100];6 intMain ()7 {8 intt,s=0;9scanf"%d",&t);Ten while(t--) One { A intn,sum0=0; - inti,a,j; -scanf"%d",&n); the intnum=0; - for(i =1; I <= N; i++) - { -scanf"%d",&a); +g[i]=INF; - if(A = =0) + { Asum0++; at Continue; - } -B[++NUM]=A-SUM0;//record the number of each number minus the previous 0 - } - intmax0=0; - for(i =1; I <= num; i++) in { - intK=lower_bound (g+1, g+num+1, B[i])-G;//To deposit the number of b[i] into a g[i], similar to the dichotomy tomax0=max0>k?max0:k;//directly records the oldest sequence length (equivalent to d[i] records the maximum length of a subsequence ending in number I, and then compares the maximum of d[i] +g[k]=B[i]; - } theprintf"Case #%d:%d\n", ++s,max0+SUM0); * } $}
Hangzhou Electric 5773 The all-purpose Zero