Cod sursa(job #3307387)

Utilizator PibooDaniel Spataru Piboo Data 20 august 2025 15:41:55
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>
using namespace std;

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

    freopen("secventa.in", "r", stdin);
    freopen("secventa.out", "w", stdout);

    int N, K;
    cin >> N >> K;
    vector<int> a(N+1);
    for (int i = 1; i <= N; i++) cin >> a[i];

    vector<int> left(N+1), right(N+1);
    stack<int> st;

    // calculează limita stângă
    for (int i = 1; i <= N; i++) {
        while (!st.empty() && a[st.top()] >= a[i]) st.pop();
        left[i] = st.empty() ? 0 : st.top();
        st.push(i);
    }

    while (!st.empty()) st.pop();

    // calculează limita dreaptă
    for (int i = N; i >= 1; i--) {
        while (!st.empty() && a[st.top()] > a[i]) st.pop();
        right[i] = st.empty() ? N+1 : st.top();
        st.push(i);
    }

    int bestVal = -30001, bestL = 0, bestR = 0;
    for (int i = 1; i <= N; i++) {
        int len = right[i] - left[i] - 1;
        if (len >= K) {
            int start = left[i] + 1;
            int end = start + K - 1;
            if (a[i] > bestVal ||
               (a[i] == bestVal && (start < bestL ||
               (start == bestL && end < bestR)))) {
                bestVal = a[i];
                bestL = start;
                bestR = end;
            }
        }
    }

    cout << bestL << " " << bestR << " " << bestVal << "\n";
    return 0;
}