Cod sursa(job #3317377)

Utilizator risxdrzBanica Albert risxdrz Data 23 octombrie 2025 15:18:44
Problema Secventa Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>
using namespace std;

int main() {
    ifstream fin("secventa.in");
    ofstream fout("secventa.out");

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

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

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

    while (!st.empty()) {
        right[st.top()] = N;
        st.pop();
    }

    int bestL = 1, bestR = K;
    int bestBase = INT_MIN;

    for (int i = 1; i <= N; ++i) {
        int len = right[i] - left[i] + 1;
        if (len >= K && a[i] > bestBase) {
            bestBase = a[i];
            bestL = left[i];
            bestR = right[i];
        }
    }

    fout << bestL << " " << bestR << " " << bestBase << "\n";
    return 0;
}