AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 4648. 最长不下降子序列

作者: 作者的头像   炽热的 ,  2022-10-21 18:04:03 ,  所有人可见 ,  阅读 105


0


#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10, M = 1e6 + 10;

int n, k;
int a[N];
struct Fenwick {
    int tr[M];
    void add(int x, int k) {
        for ( ; x < M; x += x & -x) tr[x] = max(tr[x], k);
    }
    int query(int x) {
        int res = 0;
        for ( ; x; x -= x & -x) res = max(res, tr[x]);
        return res;
    }
}f, g;

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);

    a[n + 1] = M;
    int res = k;
    for (int i = k + 2; i <= n + 1; i ++ ) {
        g.add(a[i - k - 1], g.query(a[i - k - 1]) + 1);
        f.add(a[i], f.query(a[i]) + 1);
        f.add(a[i], g.query(a[i]) + 1);
        res = max(res, f.query(a[i]) + k);
    }

    res = max(res, g.query(M - 1) + k);

    printf("%d\n", res);

    return 0;
}

0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息