Cod sursa(job #2258278)

Utilizator mihaicivMihai Vlad mihaiciv Data 11 octombrie 2018 09:52:58
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <deque>
#include <fstream>
using namespace std;

ifstream f("deque.in");
ofstream g("deque.out");
deque <long int> a;
deque <int> b;
int n, k;

int main() {
    f >> n >> k;
    long long int x;
    long long int Sum = 0;
    for (int i = 1; i <= n; ++ i) {
        f >> x;
        bool Condition = true;
        while (a.empty() == 0 && Condition) {
            if (a.back() > x) {
                a.pop_back();
                b.pop_back();
            } else {
                Condition = false;
            }
        }
        a.push_back(x);
        b.push_back(i);
        if (i >= k) {
            long long int pos = b.front();
            long long int val = a.front();
            //cout << val << " ";
            Sum += val;
            if (pos + k == i + 1 ) {
                b.pop_front();
                a.pop_front();
            }
        }
    }
    g << Sum;
    return 0;
}