Cod sursa(job #3317374)

Utilizator risxdrzBanica Albert risxdrz Data 23 octombrie 2025 15:11:52
Problema Secventa Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
using namespace std;

int a[100005], st[100005], leftp[100005], rightp[100005];

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

    int n, k, top = 0;
    fin >> n >> k;
    for (int i = 1; i <= n; i++)
        fin >> a[i];

    for (int i = 1; i <= n; i++) {
        while (top > 0 && a[st[top]] >= a[i]) {
            rightp[st[top]] = i - 1;
            top--;
        }
        if (top == 0) leftp[i] = 1;
        else leftp[i] = st[top] + 1;
        st[++top] = i;
    }

    while (top > 0) {
        rightp[st[top]] = n;
        top--;
    }

    int bestL = 1, bestR = k, bestBase = -2000000000;
    for (int i = 1; i <= n; i++) {
        int len = rightp[i] - leftp[i] + 1;
        if (len >= k && a[i] > bestBase) {
            bestBase = a[i];
            bestL = leftp[i];
            bestR = rightp[i];
        }
    }

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