Cod sursa(job #2681559)

Utilizator NeacsuMihaiNeacsu Mihai NeacsuMihai Data 5 decembrie 2020 19:41:45
Problema Secventa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;

ifstream fin ("secventa.in");
ofstream fout ("secventa.out");
deque<int>h;
int v[500001];
int main()
{
    int n, k, mx=0, i, p1, p2;
    fin>>n>>k;
    for(i=1; i<=n; i++) fin>>v[i];
    for(i=1; i<=n; i++)
    {
        while(!h.empty() && v[i]<=v[h.back()]) h.pop_back();
        h.push_back(i);

        if(h.front()==i-k) h.pop_front();

        if(i-h.front()+1>=k)
        {
            if(v[h.front()]>mx)
            {
                mx=v[h.front()];
                p1=h.front();
                p2=h.back();
            }
        }
    }
    fout<<p1<<' '<<p2<<' '<<mx;
}