Cod sursa(job #1595624)

Utilizator ciocan_catalinCiocan Catalin - Iulian ciocan_catalin Data 10 februarie 2016 14:01:55
Problema Secventa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <deque>
#define mp make_pair
using namespace std;
ifstream fin("secventa.in");
ofstream fout("secventa.out");
int n,k,a[500005],i1,i2,MAX,ind;
deque < pair <int, int> > Q;
char s[4000050];

int GET(int &ind)
{
    int semn,nr;
    nr = 0;
    if(s[ind]=='-') semn = -1,ind++;
    else semn = 1;
    while(s[ind]>='0' && s[ind]<='9')
        nr = nr*10+(s[ind++]-'0');
    while(s[ind]==' ') ind++;
    return nr*semn;
}

void Solve()
{
    int i,j,x;
    fin>>n>>k;
    fin.get();
    fin.getline(s,3500000);
    for(i = 1; i <= k; i++)
    {
        x = GET(ind);
        while(!Q.empty() && Q.back().first >= x)
            Q.pop_back();
        Q.push_back(mp(x,i));
    }
    MAX = Q.front().first;
    i1 = 1, i2 = k;
    for(i = k+1; i <= n; i++)
    {
        x = GET(ind);
        while(!Q.empty() && Q.front().second <= i-k)
            Q.pop_front();
        while(!Q.empty() && Q.back().first >= x)
            Q.pop_back();
        Q.push_back(mp(x,i));
        x = Q.front().first;
        if(x > MAX)
        {
            MAX = x;
            i2 = i;
            i1 = i-k+1;
        }
    }
    fout<<i1<<" "<<i2<<" "<<MAX<<"\n";
}
int main()
{
    Solve();
    fout.close();
    return 0;
}