序列变换 HDU - 5256

原题链接
考察:线性dp+二分
思路:
  严格上升子序列长度,每个\(a[i]-=i\),然后求非严格单调上升子序列长度.

Code

#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int n,a[N],q[N];
int main()
{
    int T, kcase = 0;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        int len = 0;
        memset(q,0,sizeof q);
        q[0] = -1e9;
        for (int i = 1; i <= n;i++)
            scanf("%d", &a[i]),a[i]-=i;
        for (int i = 1; i <= n;i++)
        {
            int l = 0, r = len;
            while(l<r)
            {
                int mid = l + r + 1 >> 1;
                if(q[mid]<=a[i])
                    l = mid;
                else
                    r = mid - 1;
            }
            q[r + 1] = a[i];
            len = max(r + 1, len);
        }
        printf("Case #%d:\n%d\n", ++kcase, n - len);
    }
    return 0;
}
posted @ 2021-07-05 02:24  acmloser  阅读(23)  评论(0编辑  收藏  举报