--- 这里是 cjiaw 的小窝(●'◡'●) ---

正在玩命加载中......
$$ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Self-defined math definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Math symbol commands \newcommand{\intd}{\,{\rm d}} % Symbol 'd' used in integration, such as 'dx' \newcommand{\diff}{{\rm d}} % Symbol 'd' used in differentiation ... $$

CF_2167_G. Mukhammadali and the Smooth Array ( LIS,树状数组 )

题目连接:Problem - 2167G - Codeforces


题目大意:

给你一个序列 ,你可以进行任意次操作,花费的代价将替换为任意整数,求将  变得单调不降的最小代价。


思路:

用树状数组记录前 i 个位置的最大值,

当遍历到 i 时,就是要找比 a


代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#include<array>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/numeric>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
#define gmap __gnu_pbds::cc_hash_table
#define power __gnu_cxx::power
using namespace std;
typedef pair<int, int> pii;

const int N = 200086, mod = 998244353;

int n, m;
int a[N], c[N];
int tr[N];

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

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

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

void solve() {

    cin >> n;
    vector<int> alls;
    
    for (int i = 1; i <= n; i++) {
        tr[i] = 0;
        cin >> a[i];
        alls.push_back(a[i]);
    }
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        sum += c[i];
    }
    
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    for (int i = 1; i <= n; i++) {
        a[i] = lower_bound(alls.begin(), alls.end(), a[i]) - alls.begin() + 1;
    }
    
    int res = 0;
    for (int i = 1; i <= n; i++) {
        int cur = query(a[i]) + c[i];
        res = max(res, cur);
        add(a[i], cur);
    }
    cout << sum - res << endl;
    
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T = 1;
    cin >> T;
    while (T--) solve();
    
    return 0;
}

 

posted @ 2026-01-21 17:07  cjiaw  阅读(0)  评论(0)    收藏  举报