Cod sursa(job #2349540)

Utilizator radubigColtos Radu radubig Data 20 februarie 2019 15:56:43
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <deque>

using namespace std;
ifstream in("deque.in");
ofstream out("deque.out");

int n,k,i;
long long s,x;
deque <long long> q;
deque <int> ind;
int main()
{
    in>>n>>k;
    for(i=1;i<=k;i++)
    {
        in>>x;
        if(q.empty())
        {
            q.push_front(x);
            ind.push_front(i);
        }
        else
        {
            while(!q.empty() && q.back()>x)
            {
                q.pop_back();
                ind.pop_back();
            }
            q.push_back(x);
            ind.push_back(i);
        }
    }
    s+=q.front();
    for(; i<=n;i++)
    {
        if(ind.front()<i-k+1)
        {
            q.pop_front();
            ind.pop_front();
        }
        in>>x;
        while(!q.empty() && q.back()>x)
        {
            q.pop_back();
                ind.pop_back();
        }
        q.push_back(x);
        ind.push_back(i);
        s+=q.front();
    }
    out<<s;
    in.close(); out.close();
    return 0;
}