0
点赞
收藏
分享

微信扫一扫

HDU 5773 The All-purpose Zero


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.


 



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


Hint

In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.



 


和正常的最长上升子序列比,只有0这一点不一样,可以模拟一下加0的操作,差不多可以看出0会使整个序列右移并且加1


这个过程是对全部的值而言的,所以可以提出来,即0的叠加单独计算,其余的按照原本的nlogn的算法来即可


#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int INF = 0x7FFFFFFF;
int T, n, a[N], f[N], cas = 0;

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
int add = 0, tot = 0;
rep(i, 1, n)
{
scanf("%d", &a[i]);
if (!a[i]) { add++; continue; }
int l = 1, r = tot;
while (l <= r)
{
int mid = l + r >> 1;
if (f[mid] + add < a[i]) l = mid + 1;
else r = mid - 1;
}
f[l] = a[i] - add;
if (l > tot) tot++;
}
printf("Case #%d: %d\n", ++cas, add + tot);
}
return 0;
}



举报

相关推荐

0 条评论