Cod sursa(job #2034605)

Utilizator Fanika123Tanasa Stefan Fanika123 Data 8 octombrie 2017 08:46:30
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>
#define in "secventa.in"
#define out "secventa.out"
using namespace std;
ifstream fin(in);
ofstream fout(out);

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

void Citire()
{
    int i;

    fin >> n >> k;
    for (i = 1; i <= n; i++)
        fin >> a[i];
}

void Rezolvare()
{
    int i,x;
    int maxi;
    int c1,c2;

    for (i = 1; i <= k; i++)
    {
        x = a[i];

        /// elimin toate elementele mai mari ca x
        while (!q.empty() && x <= a[q.back()])
            q.pop_back();

        q.push_back(i);
    }

    maxi = a[q.front()];
    c1 = q.front();
    c2 = q.back();

    for (i = k+1; i <= n; i++)
    {
        x = a[i];

        /// elimin toate elementele mai mari ca x
        while (!q.empty() && x <= a[q.back()])
            q.pop_back();

        q.push_back(i);

        /// elimin vechiul minim
        if (q.front() <= i-k) q.pop_front();

        /// actualizez maximul
        if (a[q.front()] > maxi)
        {
            maxi = a[q.front()];
            c1 = q.front();
            c2 = q.back();
        }
    }

    fout << c1 << " " << c2 << " " << maxi << "\n";
}

int main()
{
    Citire();
    Rezolvare();

    fin.close();
    fout.close();
    return 0;
}