Cod sursa(job #1804037)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 12 noiembrie 2016 10:14:11
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <deque>

using namespace std;

ifstream fin("deque.in");
ofstream fout("deque.out");

struct elem
{
    long long poz;
    long long nr;
};

long long n, k;
long long S;
deque<elem> V;

int main()
{
    int i, j, x;
    elem aux;

    fin >> n >> k;
    for (i=1;i<=k;i++)
    {
        fin >> x;
        aux.nr = x;
        aux.poz = i;
        if (V.empty())
            V.push_back(aux);
        else
        {
            while (!V.empty() && V.back().nr >= x)
                V.pop_back();
            V.push_back(aux);
        }
    }
    S+=V.front().nr;
    for (i=k+1;i<=n;i++)
    {
        fin >> x;
        aux.nr = x;
        aux.poz = i;
        if (V.front().poz+k-1 < i)
            V.pop_front();
        while (!V.empty() && V.back().nr >= x)
            V.pop_back();
        V.push_back(aux);
        S+= V.front().nr;
    }
    fout << S << '\n';
    return 0;
}