zoukankan      html  css  js  c++  java
  • hdu 5256 LIS变形

    给一个数列,问最少修改多少个元素使数列严格递增。如果不是要求“严格”递增,那就是求最长不降子序列LIS,然后n-LIS就是答案。要严格递增也好办,输入的时候用每个数减去其下标处理一下就行了。

    /*
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    const int MAXN = 100010;
    int a[MAXN];
    vector<int> v;
    int work() {
        int n;
        scanf("%d", &n);
        v.clear();
        vector<int>::iterator it;
        for (int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
            a[i] = a[i] - (i + 1); //每一个数减去其所在位置的序号
        }
        v.push_back(a[0]);
        for (int i = 1; i < n; i++) {
            if (a[i] >= *(v.end() - 1)) {
                v.push_back(a[i]);
            } else {
                it = upper_bound(v.begin(), v.end(), a[i]);
                *(it) = a[i];
            }
        }
        return n - v.size();
    }
    int main() {
        int T;
        scanf("%d", &T);
        for (int t = 1; t <= T; t++) {
            printf("Case #%d:
    %d
    ", t, work());
        }
        return 0;
    }
  • 相关阅读:
    Hello,Cnblogs,I'm Kxia
    运维
    CRT 操作数据库乱码
    STM32 各引脚功能
    遍历结果集
    更换税控服务器主板后,重新申请注册码
    修改远程桌面端口号
    nginx 设置开机启动
    windows 日志清理批处理 设置到计划任务就可以每天清理日志了
    按关键字查找文件
  • 原文地址:https://www.cnblogs.com/moonbay/p/4542512.html
Copyright © 2011-2022 走看看