Cod sursa(job #2701994)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 2 februarie 2021 15:46:36
Problema Secventa Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, k, a[500005], M;
deque<int>q;

int main()
{
    int i, p, x;
    fin >> n >> k;
    for(i=1; i<=n; i++)
        fin >> a[i];
    for(i=1; i<=k; i++)
    {
        x=a[i];
        while(!q.empty()&&a[q.back()]>=x)
            q.pop_back();
        q.push_back(i);
    }
    M=a[q.front()];
    p=i;
    for(i=k+1; i<=n; i++)
    {
        x=a[i];
        while(!q.empty()&&a[q.back()]>=x)
            q.pop_back();
        q.push_back(i);
        if(i-k==q.front()) q.pop_front();
        if(a[q.front()]>M)
        {
            M=a[q.front()];
            p=i;
        }
    }
    fout << p-k+1 << " " << p << " " << M;
    return 0;
}