Cod sursa(job #984310)

Utilizator Johny_Depp22Johnny Depp Johny_Depp22 Data 14 august 2013 00:55:56
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <deque>
using namespace std;

ifstream f("secventa.in");
ofstream g("secventa.out");

deque < int > D;
int a[500005], n, k, inc, sf, minim=-1000000;
string s;

int next()
{
    int semn=1, nr=0;
    if (s[ind]=='-') semn=-1,ind++;
    while (s[ind]!=' ') nr=nr*10+s[ind]-'0', ind++;
    ind++;
    return semn*nr;
}

int main()
{
    f>>n>>k;
    f.get(); getline(f,s); s+=' ';
    for (i=1; i<=n; i++) a[i]=next();

    for (int i=1; i<=n; i++)
    {
        while (D.size() && D.front()<=i-k) D.pop_front();
        while (D.size() && a[i]<=a[D.back()]) D.pop_back();
        D.push_back(i);

        if (i>=k && minim<a[D.front()]) minim=a[D.front()], inc=i-k+1, sf=i;
    }
    g<<inc<<' '<<sf<<' '<<minim<<'\n';
    return 0;
}