Submission #637873

#TimeUsernameProblemLanguageResultExecution timeMemory
637873BlancaHMRabbit Carrot (LMIO19_triusis)C++14
63 / 100
1084 ms3124 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void updateTree(int x, int L, int R, int ind, int newVal, vector<int> & seg) {
    if (ind < L || ind > R) {
        return;
    } else if (L == R) {
        seg[x] = newVal;
    } else {
        int mid = L + (R-L)/2;
        updateTree(2*x, L, mid, ind, newVal, seg);
        updateTree(2*x+1, mid+1, R, ind, newVal, seg);
        seg[x] = max(seg[2*x], seg[2*x+1]);
    }
}

int queryTree(int x, int L, int R, int startInd, int endInd, vector<int> & seg) {
    if (startInd > R || endInd < L) {
        return 0;
    } else if (startInd <= L && endInd >= R) {
        return seg[x];
    } else {
        int mid = L + (R-L)/2;
        int leftAns = queryTree(2*x, L, mid, startInd, endInd, seg);
        int rightAns = queryTree(2*x+1, mid+1, R, startInd, endInd, seg);
        return leftAns+rightAns;
    }
}

/*
int lnds(vector<int> & L) {
    // returns the length of the longest non-decreasing subsequence of L
    int N = (int) L.size();
    if (N == 0)
        return 0;
    vector<pair<int, int>> indL(N);
    for (int i = 0; i < N; i++) {
        indL[i] = make_pair(L[i], i);
    }
    sort(indL.begin(), indL.end());
    vector<int> DP(N, 0);
    vector<int> seg(4*N, 0);
    updateTree(1, 0, N-1, 0, 1, seg);
    for (int i = 1; i < N; i++) {
        int curPos = indL[i].second;
        int bestPrev = queryTree(1, 0, N-1, 0, curPos-1, seg);
        updateTree(1, 0, N-1, curPos, bestPrev+1, seg);
    }
    return queryTree(1, 0, N-1, 0, N-1, seg);
}
*/

int lnds(vector<int> & L) {
    int N = (int) L.size();
    if (N == 0) {
        return 0;
    }
    vector<int> DP(N, 0);
    DP[0] = 1;
    int ans = 1;
    for (int i = 1; i < N; i++) {
        for (int j = 0; j < i; j++) {
            if (L[i] >= L[j]) {
                DP[i] = max(DP[i], DP[j]+1);
            }
        }
        ans = max(ans, DP[i]);
    }
    return ans;
}

int minChanges(vector<int> & heights, int M) {
    int N = (int) heights.size();
    vector<int> altered;
    for (int i = 0; i < N; i++) {
        if (M*(i+1) - heights[i] >= 0) {
            altered.push_back(M*(i+1) - heights[i]);
        }
    }
    return N - lnds(altered);
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> heights(N);
    for (int i = 0; i < N; i++) {
        cin >> heights[i];
    }
    cout << minChanges(heights, M) << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...