2016多校训练赛第4场第十题(hdu5773——The All-purpose Zero) (简单)

Question: ?? 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.

题目大意:求给定的数组的LIS,其中0可以变为任何数。

题解:对于求LIS的问题,我们有一个O(nlogn)的方法。维护一个单增序列,其第i位代表当前前k位中长度为i的LIS中最小的末位是多少。如果碰到0,则令每个当前串的长度加一,且最后一位加1。做一遍即可知道答案。

#include <cstdio>
#include <iostreaM>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int n,a[maxn];
int dp[maxn];
int cnt = 0;
int main()
{
    freopen("10.in","r",stdin);
    freopen("10.out","w",stdout);
    int kase;scanf("%d",&kase);
    for(int t = 1;t<=kase;t++){
        cnt = 0;dp[0]=-1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++){
            if(a[i]!=0) {
                int p = lower_bound(dp,dp+cnt+1,a[i]) - dp;
                if(p>cnt) cnt++;
                dp[p]=a[i];
            }
            else {
                cnt++;
                for(int i=cnt;i>=1;i--) {
                    dp[i]=dp[i-1]+1;
                }
            }
        }
        printf("Case #%d: %d\n",t,cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值