Cod sursa(job #984712)

Utilizator manutrutaEmanuel Truta manutruta Data 15 august 2013 10:26:05
Problema Secventa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
# include <iostream>
# include <fstream>
# include <deque>
using namespace std;

# define MAXN 500010

//<parsare>
FILE* fin=fopen("secventa.in","r");
const unsigned maxb=8192;
char buf[maxb];
unsigned ptr=maxb;

inline int getInt(){
    int nr=0,mul=1;
    while(buf[ptr]<'0'||'9'<buf[ptr]||buf[ptr]=='-'){
        if(buf[ptr]=='-'){
            mul=-1;
        }
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    while('0'<=buf[ptr]&&buf[ptr]<='9'){
        nr=nr*10+buf[ptr]-'0';
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    return nr*mul;
}
//</parsare>

ofstream g("secventa.out");

int n, k;
int a[MAXN];
deque<int> coada;

int main()
{
    int sol = -30010, pi, pf;
    int n = getInt(), k = getInt();
    for(int i = 1; i <= n; i++){
        a[i] = getInt();
    }

    for (int i = 1; i <= n; i++) {
        while (!coada.empty() && a[i] <= a[coada.back()]) {
            coada.pop_back();
        }

        coada.push_back(i);

        if (coada.front() == i - k) {
            coada.pop_front();
        }

        if (i >= k && a[coada.front()] > sol) {
            sol = a[coada.front()];
            pi = i - k + 1;
            pf = i;
        }
    }

    g << pi << ' ' << pf << ' ' << sol;

    return 0;
}