2016多校联赛4D The All-purpose Zero(hdu 5773)


The All-purpose Zero

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 12    Accepted Submission(s): 4


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
  
  
Case #1: 5 Case #2: 5
Hint
In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
 

Author
FZU
 

Source
 

Recommend
wange2014
题意:就是求最长严格递增子序列,但是因为0可以任意变,所以难度大大增加。

思路:这道题肯定是用dp的,一般的思路是碰上0,就把dp数组的值变成前一个dp的值再加1,但是这样是会超时的。所以我们就要用sum统计0的个数,这时我们就不需要把dp数组改变,我们可以通过sum来确定现在的dp值,例如,正确的dp[i]实际是=dp[i-sum]+sum,如果i-sum<1,默认为无穷小。这样就回到了求普通的最长严格递增子序列了,下面给代码。

#include<iostream>   
#include<cstring>  
#include<cstdio>  
using namespace std;
const int maxn = 100005;
int dp[maxn], a[maxn], n, len;
int sumnum = 0;
int binarysearch(int value)//找到第一个大于或等于自己的数的位置   
{
	int left = 1, right = len - sumnum-1, mid;
	while (left<right)
	{
		mid = (left + right) >> 1;
		if (value>dp[mid])left = mid + 1;
		else right = mid - 1;
	}
	if (dp[left]<value)return left + 1;
	return left;
}
int main()
{
	int T;
	cin >> T;
	for (int tcase = 1; tcase <= T;tcase++)
	{
		cin >> n;
		for (int i = 1; i <= n; i++)cin >> a[i];
		len = 1;
		dp[len] = a[1];
		sumnum = 0;
		for (int i = 2; i <= n; i++)
		{
			if (a[i] == 0) {
				sumnum++;
				len++;
			}
			else if (len  - sumnum <= 0) {
				len++;
				continue;
			}
			else if (a[i] > dp[len-sumnum]+sumnum)
			{
				dp[(++len-sumnum)] = a[i]-sumnum;
			}
			else
			{
				int pos = binarysearch(a[i]-sumnum);
				//printf("a[%d]=%d",i,a[i]);  
				//printf("    pos=%d\n",pos);  
				dp[pos] = a[i]-sumnum;
				//printf("  so  dp[%d]=%d\n",pos,dp[pos]);  
			}
			//printf("dp[%d]=%d\n",len,dp[len]);  

		}
		printf("Case #%d: %d\n", tcase, len);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值