[树状数组] aw3662. 最大上升子序列和(LIS优化+树状数组+离散化+好题+aw周赛003_3)

1. 题目来源

链接:3662. 最大上升子序列和

相关题目:[线性dp] 最大上升子序列和(最长上升子序列模型+经典)

2. 题目解析

LIS+树状数组+离散化。

看题解就行了:抽风大佬的题解

树状数组已经忘了,应该 2 各月没写相关题目了… 但分析来确实是很经典的优化。


很不错的题目,很综合,很有用!


时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)

空间复杂度: O ( n ) O(n) O(n)


y总代码:

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

using namespace std;

typedef long long LL;

const int N = 100010;

int n;
int w[N];
LL tr[N];
vector<int> xs;
LL f[N];

int get(int x) {
    return lower_bound(xs.begin(), xs.end(), x) - xs.begin() + 1;
}

int lowbit(int x) {
    return x & -x;
}

void add(int x, LL v) {
    for (int i = x; i <= n; i += lowbit(i))
        tr[i] = max(tr[i], v);
}

LL query(int x) {
    LL res = 0;
    for (int i = x; i; i -= lowbit(i))
        res = max(res, tr[i]);
    return res;
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) {
        scanf("%d", &w[i]);
        xs.push_back(w[i]);
    }
    
    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());

    LL res = 0;
    for (int i = 0; i < n; i ++ ) {
        int k = get(w[i]);
        f[i] = query(k - 1) + w[i];
        res = max(res, f[i]);
        add(k, f[i]);
    }

    printf("%lld\n", res);
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值