Cod sursa(job #2938184)

Utilizator Kawaiimeatball13Russu Mihaela Kawaiimeatball13 Data 11 noiembrie 2022 19:17:57
Problema Secventa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;

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

int n, k;
int a[500001];
deque <int> d;

void citire()
{
    fin >> n >> k;
    for(int i = 0; i < n; ++i)
        fin >> a[i];
}

void rezolvare()
{
    int st, fin, vmax = -30001;

    for(int i = 0; i < n; ++i)
    {
        while(!d.empty() && a[i] < a[d.back()])
            d.pop_back();
        d.push_back(i);
        if(d.front() <= i - k)
            d.pop_front();
        if(i >= k - 1)
        {
            if(a[d.front()] > vmax)
            {
                vmax = a[d.front()];
                st = d.front();
                fin = i;
            }
        }
    }
    fout << st + 1 << ' ' << fin + 1 << ' ' << vmax;
}

int main()
{
    citire();
    rezolvare();
    return 0;
}