Cod sursa(job #1540719)

Utilizator CraiuAndrei Craiu Craiu Data 3 decembrie 2015 09:31:10
Problema Secventa Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

int n, k;
int a[500005];

deque <int> q;

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

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

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