HDU 5773 The All-purpose Zero [dp][lis]

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

普通的lis就不讲了,这道题我用n^2的方法t了;学习了一下nlogn的方法;
给个板子

int lis(int len,int *a)//所求数列长度和数列,返回lis
{
    memset(dp,inf,sizeof(dp));
    for(int i=0;i<len;i++)
        *lower_bound(dp,dp+len,a[i])=a[i];
    return (lower_bound(dp,dp+len,inf)-dp);
}

这道题加了0可以填数的设定,因为0可以变成任何数,所以尽量多的加入0是一定正确的;
因为0可以变成任何数,如果0的两边的差大于等于0的个数且右边大或者小,自然可以在去掉0的lis上直接加上0的个数,如果两边差比0小,可以看成将0变成右边的某些数而不选右边的某些数;因此得出结论可以直接加上所有0的个数,为了右边不重复加某些数,每有一个0就将他右面的所有数减一,从而可以避免再选一次同样的数;
综上,只要预处理以后直接求lis然后加上0 的个数就是答案了

#include <iostream>
#include <cmath>
#include<queue>
#include <map>
#include <stdio.h>
#include<cstring>
#include <algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=100005;
int dp[maxn],a[maxn];
int lis(int len,int *a)
{
    memset(dp,inf,sizeof(dp));
    for(int i=0;i<len;i++)
        *lower_bound(dp,dp+len,a[i])=a[i];
    return (lower_bound(dp,dp+len,inf)-dp);
}
int main()
{
    int b,n,N,tem,les,ans;
    cin>>N;
    for(int k=1;k<=N;k++)
    {
        cin>>n;
        ans=tem=les=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b);
            if(b==0) les++;
            else a[tem++]=b-les;
            dp[i]=1;
        }
        n=tem;
        ans=lis(n,a);
        printf("Case #%d: %d\n",k,ans+les);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值