hdoj 5256 序列变换 【LIS 变形】

题目链接:hdoj 5256 序列变换

序列变换

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1184 Accepted Submission(s): 439

Problem Description
我们有一个数列A1,A2…An,你现在要求修改数量最少的元素,使得这个数列严格递增。其中无论是修改前还是修改后,每个元素都必须是整数。
请输出最少需要修改多少个元素。

Input
第一行输入一个T(1≤T≤10),表示有多少组数据

每一组数据:

第一行输入一个N(1≤N≤105),表示数列的长度

第二行输入N个数A1,A2,…,An。

每一个数列中的元素都是正整数而且不超过106。

Output
对于每组数据,先输出一行

Case #i:

然后输出最少需要修改多少个元素。

Sample Input
2
2
1 10
3
2 5 4

Sample Output
Case #1:
0
Case #2:
1

队友写了个O(n^2)的代码,还牛逼哄哄的交了。当时想干死他,看了他的代码a[i] - a[j] >= i - j,灵感来就过了。。。

思路:a[i] - a[j] >= i - j -> a[i] - a[i] >= a[j] - j。然后就是求最长不下降子序列。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) { x += y; x %= MOD; }
int a[MAXN], g[MAXN], dp[MAXN];
int main()
{
    int t, kcase = 1; scanf("%d", &t);
    while(t--) {
        int N; scanf("%d", &N);
        for(int i = 1; i <= N; i++) {
            scanf("%d", &a[i]);
            a[i] = a[i] - i;
        }
        CLR(g, INF); int ans = 0;
        for(int i = 1; i <= N; i++)
        {
            int k = upper_bound(g+1, g+N+1, a[i]) - g;
            dp[i] = k; ans = max(ans, k);
            g[dp[i]] = min(g[dp[i]], a[i]);
        }
        printf("Case #%d:\n%d\n", kcase++, N - ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值