2016多校训练Contest4: 1010 The All-purpose Zero hdu5773

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.

我们可以发现,对于最大的ans,必定有一种情况可以做到取了所有的0

比如4 0 5这种情况。我们可以把0变成5而放弃后面一个5

这样的话就可以去掉0,在总答案上加上0的总数

可是题目要求严格单调增,所以我们需要把后面的数变成不能转移的状态

所以每个数减去前面0的个数,然后直接跑LIS即可

【考场上突然不会写LIS了...下面是队友写的程序】

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 100010;
int num[N];
int main(void){
    int t,ti;
    int n;
    int i,j,k;
    int r,p;
    int cas = 1;
    int cnt;
    scanf("%d",&t);
    for(ti=1;ti<=t;ti++){
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%d",&r);
            num[i] = r;
        }
        cnt = 0;
        for(i=1,j=1;i<=n;i++){
            if(num[i]==0){
                cnt++;
            }
            else{
                num[i] = num[i]-cnt;
                num[j++] = num[i];
            }
        }
        n = n-cnt;
        int len = 1;
        if(n==0){
            len = 0;
        }
        for(i=2;i<=n;i++){
            int l = 0,r = len;
            while(l<r){
                int mid = (l+r+1)/2;
                if(num[mid]<num[i]){
                    l = mid;
                }
                else{
                    r = mid - 1;
                }
            }
            if(l==len){
                len = l+1;
            }
            num[l+1] = num[i];
        }
        printf("Case #%d: %d\n",ti,len+cnt);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值