제출 #1343991

#제출 시각아이디문제언어결과실행 시간메모리
1343991po_rag526Rabbit Carrot (LMIO19_triusis)C++17
100 / 100
19 ms5004 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    // Fast I/O
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int N;
    long long M;
    if (!(cin >> N >> M)) return 0;

    vector<long long> y;
    for (int i = 1; i <= N; ++i) {
        long long h;
        cin >> h;
        
        // Only consider poles that could potentially be reached from height 0
        if (h <= (long long)i * M) {
            y.push_back((long long)i * M - h);
        }
    }

    // Find the Longest Non-Decreasing Subsequence (LNDS) of y
    // We use the standard O(N log N) tails approach
    vector<long long> tails;
    for (long long val : y) {
        // upper_bound finds the first element > val
        // This allows us to maintain a non-decreasing sequence
        auto it = upper_bound(tails.begin(), tails.end(), val);
        if (it == tails.end()) {
            tails.push_back(val);
        } else {
            *it = val;
        }
    }

    // Smallest modifications = Total poles - Max poles kept
    cout << N - (int)tails.size() << endl;

    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...