Cod sursa(job #2107557)

Utilizator toadehuPuscasu Razvan Stefan toadehu Data 17 ianuarie 2018 15:25:23
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>

#define mp make_pair

using namespace std;

deque <pair<int,int> > dq;

int n,v[5000011],k;
long long int suma;

int main()
{
    ifstream fin ("deque.in");
    ofstream fout ("deque.out");
    fin>>n>>k;
    for (int i=1;i<=k;++i)
    {
        fin>>v[i];
        pair <int,int> x = mp(v[i],i);
        while (!dq.empty() && dq.back().first>=v[i])
        {
            dq.pop_back();
        }
        dq.push_back(x);
    }
    suma+=dq.front().first;
    //cout<<dq.front().first<<" ";
    for (int i=k+1;i<=n;++i)
    {
        while (dq.front().second<=i-k)
        {
            dq.pop_front();
        }
        fin>>v[i];
        pair <int,int>x=mp(v[i],i);
        while (!dq.empty() && (dq.back().first>=v[i]))
        {
            dq.pop_back();
        }
        while (dq.front().second<=i-k)
        {
            dq.pop_front();
        }
        dq.push_back(x);
        suma+=dq.front().first;
   // cout<<dq.front().first<<" ";
    }
    fout<<suma;
}