Cod sursa(job #3307377)

Utilizator gugalcromMuntoiu Vlad-Ioan gugalcrom Data 20 august 2025 15:33:52
Problema Secventa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <vector>
#include <deque>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    ifstream fin("secventa.in");
    ofstream fout("secventa.out");
    int N, M;
    fin >> N >> M;
    vector<int> a(N);
    deque<int> dq(N);
    for(int i=0; i<N; ++i) {
        fin >> a[i];
    }
    for(int i=0; i<M-1; ++i) {
        while(!dq.empty() && a[dq.back()] >= a[i]) {
            dq.pop_back();
        }
        dq.push_back(i);
    }
    int mx = 0, imx = 0;
    for(int i=M-1; i<N; ++i) {
        while(!dq.empty() && dq.front() <= i-M) {
            dq.pop_front();
        }
        while(!dq.empty() && a[dq.back()] >= a[i]) {
            dq.pop_back();
        }
        dq.push_back(i);
        if(mx < a[dq.front()]) {
            mx = a[dq.front()];
            imx = i;
        }
        //cout << i << ' ' << a[dq.front()] << '\n';
    }
    fout << imx - M + 2 << ' ' << imx + 1 << ' ' << mx << '\n';
    return 0;
}