187292025-11-01 11:59:15PKBLapozgatáscpp17Accepted 100/10043ms2036 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll M, K; int N;
    if (!(cin >> M >> K >> N)) return 0;
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) cin >> A[i];

    map<ll,ll> seg; // start-end intervallum
    seg[K] = K;
    ll curr = K, total = 0;
    int starts = 0;

    auto seen = [&](ll x)->bool{ //lambda
        auto it = seg.upper_bound(x);
        if (it == seg.begin()) return false;
        --it;
        return it->first <= x && x <= it->second;
    };

    for (ll t : A) {
        if (seen(t)) continue;
        ++starts;
        total += llabs(curr - t);

        ll L = min(curr, t), R = max(curr, t);
        auto it = seg.lower_bound(L);
        if (it != seg.begin()) {
            --it;
            if (it->second < L - 1) ++it;
        }


        // összeolvasztás
        while (it != seg.end() && it->first <= R + 1) {
            L = min(L, it->first);
            R = max(R, it->second);
            it = seg.erase(it);
        }
        seg[L] = R;
        curr = t;
    }

    cout << starts << " " << total << '\n';
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1ms316 KiB
subtask212/12
3Accepted1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms316 KiB
subtask324/24
6Accepted1ms328 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
9Accepted1ms508 KiB
10Accepted1ms316 KiB
11Accepted1ms508 KiB
subtask428/28
12Accepted41ms1844 KiB
13Accepted39ms1844 KiB
14Accepted37ms1844 KiB
15Accepted34ms1848 KiB
16Accepted32ms2028 KiB
17Accepted29ms2036 KiB
18Accepted29ms1848 KiB
subtask536/36
19Accepted43ms1844 KiB
20Accepted41ms1844 KiB
21Accepted39ms2028 KiB
22Accepted37ms2036 KiB
23Accepted35ms1844 KiB
24Accepted32ms1772 KiB
25Accepted30ms1844 KiB
26Accepted30ms1968 KiB
27Accepted29ms1844 KiB