Cod sursa(job #2362386)

Utilizator BGondaGonda George Luis Bogdan BGonda Data 3 martie 2019 10:31:29
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <deque>
using namespace std;
ifstream fin ("secventa.in");
ofstream fout ("secventa.out");
deque <int> Q;
int a[500005];
void Get(int &x);
int main()
{
    int n,k,i,maxi=-30001,poz;
    Get(n);
    Get(k);
    a[0] = a[n+1]=-30001;
    for (i = 1; i <= n; ++i)
    {
        Get(a[i]);
        while (!Q.empty() && Q.front()<i-k+1)
            Q.pop_front();
        while (!Q.empty() && a[Q.back()]>a[i])
            Q.pop_back();
        Q.push_back(i);
        if (i >= k)
            if (a[Q.front()]>maxi)
            {
                maxi=a[Q.front()];
                poz=Q.front();
            }
    }
    i = poz;
    while (a[i] >= maxi)
        i--;
    i++;
    fout<<i<<" "<<i+k-1<<" "<<maxi;
    return 0;
}
const int Lim = 1 << 20;
int p1 =  Lim - 1;
char s[Lim];

void Next()
{
    if (++p1 == Lim)
        fin.get(s, Lim + 1, EOF), p1 = 0;
}

void Get(int &x)
{
    while (s[p1] < '0' || s[p1] > '9')
    {
        if (s[p1] == '-')
            break;
        Next();
    }

    int sgn = 1;
    if (s[p1] == '-')
    {
        sgn = -1;
        Next();
    }

    for (x = 0; s[p1] >= '0' && s[p1] <= '9'; Next())
        x = x * 10 + s[p1] - '0';
    x *= sgn;
}