Cod sursa(job #1769847)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 3 octombrie 2016 11:50:33
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>
#define Nmax 500005

using namespace std;

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

void Citire()
{
    ifstream f("secventa.in");
    f >> n >> k;
    int i;
    for(i = 1; i <= n; i++)
        f >> a[i];
    f.close();
}

void Rezolv()
{
    int i, st, dr, nmax, x;
    for(i = 1; i <= k; i++)
    {
        x = a[i];
        while(!q.empty() && a[q.back()] >= x)
            q.pop_back();
        q.push_back(i);
    }
    nmax = q.front();
    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 - q.front() >= k)
            q.pop_front();
        if(nmax < a[q.front()])
        {
            nmax = a[q.front()];
            st = i - k + 1;
            dr = i;
        }
    }
    ofstream g("secventa.out");
    g << st << " " << dr << " " << nmax << "\n";
    g.close();
}

int main()
{
    Citire();
    Rezolv();
    return 0;
}