18502022-12-05 11:05:42kovacs.peter.18fFasor (40)cpp11Accepted 40/4030ms5116 KiB
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int main() {
    cin.sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<int> treeS(N);
    vector<bool> goodS(N);
    stack<pair<int, int>> higherS; // height, index
    for (int i = 0; i < N; i++) {
        cin >> treeS[i];
        while (!higherS.empty() && higherS.top().first <= treeS[i]) {
            higherS.pop();
        }
        goodS[i] = higherS.empty() || higherS.top().second + K < i;
        higherS.push({ treeS[i], i });
    }
    while (!higherS.empty()) {
        higherS.pop();
    }
    int answer = -2;
    for (int i = N - 1; i >= 0; i--) {
        while (!higherS.empty() && higherS.top().first <= treeS[i]) {
            higherS.pop();
        }
        if (goodS[i] && (higherS.empty() || higherS.top().second - K > i)) {
            answer = i;
        }
        higherS.push({ treeS[i], i });
    }
    cout << answer + 1 << '\n';
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1700 KiB
2Accepted0/03ms2036 KiB
3Accepted2/22ms2184 KiB
4Accepted2/22ms2144 KiB
5Accepted2/22ms2272 KiB
6Accepted2/22ms2480 KiB
7Accepted2/22ms2836 KiB
8Accepted2/23ms2948 KiB
9Accepted2/23ms3244 KiB
10Accepted2/24ms3124 KiB
11Accepted2/23ms3120 KiB
12Accepted2/23ms3180 KiB
13Accepted2/214ms3768 KiB
14Accepted2/214ms3764 KiB
15Accepted2/224ms4484 KiB
16Accepted2/226ms4816 KiB
17Accepted2/228ms4880 KiB
18Accepted2/227ms4944 KiB
19Accepted2/227ms5116 KiB
20Accepted2/212ms4296 KiB
21Accepted2/225ms4940 KiB
22Accepted2/230ms4944 KiB