Submission #1327173

#TimeUsernameProblemLanguageResultExecution timeMemory
1327173hoangtien69Rabbit Carrot (LMIO19_triusis)C++20
0 / 100
2 ms568 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 5;

int n, M;
int a[MAXN];
int dp[MAXN];
vector<int> comp;
int st[4 * MAXN];

void update(int id, int l, int r, int pos, int val)
{
    if (l == r)
    {
        st[id] = max(st[id], val);
        return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid)
        update(id<<1, l, mid, pos, val);
    else
        update(id<<1|1, mid+1, r, pos, val);
    st[id] = max(st[id<<1], st[id<<1|1]);
}
int get(int id, int l, int r, int u, int v)
{
    if (v < l || r < u) return 0;
    if (u <= l && r <= v) return st[id];
    int mid = (l + r) >> 1;
    return max(get(id<<1, l, mid, u, v),
               get(id<<1|1, mid+1, r, u, v));
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> M;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        comp.push_back(a[i]);
    }
    sort(comp.begin(), comp.end());
    comp.erase(unique(comp.begin(), comp.end()), comp.end());
    int sz = comp.size();
    int maxLen = 0;
    for (int i = 1; i <= n; i++)
    {
        int Lval = a[i] - M;
        int L = lower_bound(comp.begin(), comp.end(), Lval) - comp.begin();
        int R = sz - 1;
        int best = 0;
        if (L <= R)
            best = get(1, 0, sz-1, L, R);

        if (a[i] <= M)
            best = max(best, 0LL);
        if (best == 0 && a[i] > M)
        {
            dp[i] = 0;
        }
        else
        {
            dp[i] = best + 1;
            int pos = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
            update(1, 0, sz-1, pos, dp[i]);
            maxLen = max(maxLen, dp[i]);
        }
    }
    cout << n - maxLen;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...