18502022-12-05 11:05:42kovacs.peter.18fFasor (40)cpp11Elfogadva 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';
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms1700 KiB
2Elfogadva0/03ms2036 KiB
3Elfogadva2/22ms2184 KiB
4Elfogadva2/22ms2144 KiB
5Elfogadva2/22ms2272 KiB
6Elfogadva2/22ms2480 KiB
7Elfogadva2/22ms2836 KiB
8Elfogadva2/23ms2948 KiB
9Elfogadva2/23ms3244 KiB
10Elfogadva2/24ms3124 KiB
11Elfogadva2/23ms3120 KiB
12Elfogadva2/23ms3180 KiB
13Elfogadva2/214ms3768 KiB
14Elfogadva2/214ms3764 KiB
15Elfogadva2/224ms4484 KiB
16Elfogadva2/226ms4816 KiB
17Elfogadva2/228ms4880 KiB
18Elfogadva2/227ms4944 KiB
19Elfogadva2/227ms5116 KiB
20Elfogadva2/212ms4296 KiB
21Elfogadva2/225ms4940 KiB
22Elfogadva2/230ms4944 KiB