您的位置:首页 > 其它

HDU 5773 The All-purpose Zero(LIS)

2016-08-12 21:23 435 查看
The All-purpose Zero

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1592 Accepted Submission(s): 763

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

给你一个数列,零可以改变成任何值,可以是负数,求最长子序列。

这里我们肯定是要考虑一下零的出现对结果的影响了,先用 zeron 记录一下零出现的次数,因为零可以变的无线小,所以要定义一个 dp[0] = -INF,以便让后面的数在变幻的时候有多余的空间。出现零的地方要使得最大的序列的增加一个单位,这个单位的值把最长序列的的最小值加一就好了(保证最小),载看有多少个零的存在,依次递减赋值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 100010
#define INF 0x3f3f3f3f

int a[M], dp[M], n;
void init()
{
for(int i=1; i<=n; i++)
{
dp[i] = INF;
}
}
int main()
{
int t;
scanf("%d", &t);
for(int ca=1; ca<=t; ca++)
{
scanf("%d", &n);
int zeron = 0;
for(int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
}
init();
int maxs = 0, j = 0;
dp[0] = -INF;
dp[1] = a[1];
maxs = 1;
for(int i=2; i<=n; i++)
{
if(a[i] == 0)//判断是不是零
{
for(int i=maxs; i>=zeron; i--)
{
dp[i+1] = dp[i] + 1;//按次序递减
}
maxs++;
zeron++;
continue;
}
j = lower_bound(dp+1, dp+n+1, a[i]) - dp;
dp[j] = a[i];
maxs = max(maxs, j);
}
printf("Case #%d: %d\n", ca, maxs);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: