The All-purpose Zero---hdu5773(LIS变形)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773

题意: 给出n个数,其中 0 可当作任何数,求能够得到的最长上升子序列(严格上升)的长度;

我们可以假设所有的零都能用上,那么结果一定包含零的个数,然后就把那些非零的数都减去它前面零的个数,然后求这些数的最长上升子序列的长度即可;

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define mod 1000000007
typedef long long LL;
//////////////////////////////////////////////////////////////
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;

int a[N], b[N], dp[N], n;

int main()
{
    int T, t = 1;
    scanf("%d", &T);
    while(T--)
    {
        met(a, 0); met(b, 0);

        scanf("%d", &n);
        int m = 0, cnt = 0;

        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
            if(a[i] == 0) cnt ++;
            else b[m++] = a[i]-cnt;
        }

        met(dp, INF);

        for(int i=0; i<m; i++)
        {
            int pos = lower_bound(dp, dp+m, b[i]) - dp;
            dp[pos] = b[i];
        }
        int ans = lower_bound(dp, dp+m, INF) - dp;

        printf("Case #%d: %d
", t++, ans+cnt);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5967011.html