hdu 5256 序列变换 LIS 变型 思维转化

hdu 5256 序列变换 LIS 变型

题目链接:  hdu 5256 序列变换
题意:有一个数列A1,A2...An,要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。输出最少需要修改多少个元素。 N(1≤N≤10^5)
分析:题目乍一看跟LIS很像,确实是的。只是不过多了个限制,严格递增。我们可以这样考虑:a[i]-a[j]>=i-j,i>j,a[i],a[j]∈LIS,即a[i]-i>=a[j]-j。然后将原序列做个变换,a[i] = a[i]-i, 这样就去掉了位置的影响了,然后求最长非降子序列。N - 最长非降子序列的长度len 就是答案了。
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

using namespace std;

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
typedef __int64         LL;
typedef pair<int, int>  PII;
const int MAXN = 1e5 + 5;

int T, N;
int A[MAXN], F[MAXN];

int LIS() {
    int len = 0;
    F[len ++] = A[0];
    for (int i = 1; i < N; i++) {
        if(A[i] >= F[len - 1]) {
            F[len ++] = A[i];
        } else {
            int pos = upper_bound(F, F + len, A[i]) - F;
            F[pos] = A[i];
        }
    }
    return len;
}

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    int cas = 0;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &N);
        for (int i = 0; i < N; i++) {
            scanf("%d", &A[i]);
            A[i] -= i;
        }
        int len = LIS(), res;
        res = N - len;
        printf("Case #%d:\n", ++cas);
        printf("%d\n", res);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值