Cod sursa(job #1754962)

Utilizator meriniucrMeriniuc Razvan- Dumitru meriniucr Data 9 septembrie 2016 01:08:32
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <deque>

using ll = long long;

std::deque <ll> q;
ll x[5000000];

int main()
{
    ll n;
    ll index;
    ll k;
    ll sum;

    std::ifstream mama("deque.in");
    std::ofstream tata("deque.out");

    mama >> n;
    mama >> k;

    for (index = 0; index < k; ++index)
    {
        mama >> x[index];

        while ( (not q.empty()) and
                (q.back() > x[index]) )
        {
            q.pop_back();
        }

        q.push_back(x[index]);
    }

    sum = q.front();

    for (; index < n; ++index)
    {
        mama >> x[index];

        while ( (not q.empty()) and
                (q.back() > x[index]) )
        {
            q.pop_back();
        }

        q.push_back(x[index]);

        if (x[index - k] == q.front())
        {
            q.pop_front();
        }

        sum += q.front();
    }

    tata << sum;
    return 0;
}