Cod sursa(job #2618113)

Utilizator STEFAN-ZOTAZota Stefan-Daniel STEFAN-ZOTA Data 23 mai 2020 18:17:38
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include<bits/stdc++.h>

using namespace std;
ifstream f("queue.in");
ofstream g("queue.out");
deque<pair<int,int>>coada;
int n,k,i;
int v[5000001];
long long sumaMin;
int main()
{
    f>>n>>k;
    deque<pair<int,int>>coada;

    for(i = 1; i <= n; i++)
        f>>v[i];
    coada.push_back({v[1], 1});

    for(i = 2; i <= k; i++)
    {
        while(coada.back().first >= v[i])
            if(coada.size() != 0)
                coada.pop_back();
            else break;
        coada.push_back({v[i], i});
    }

    sumaMin = coada.front().first;
    for(i = k+1; i <= n; i++)
    {
        while(coada.back().first >= v[i])
            if(coada.size() != 0)
                coada.pop_back();
            else break;
        coada.push_back({v[i], i});

        if(coada.front().second == i-k)
            coada.pop_front();
        sumaMin += coada.front().first;
    }
    g<<sumaMin;

    return 0;
}