Cod sursa(job #2254362)

Utilizator mihaicivMihai Vlad mihaiciv Data 5 octombrie 2018 09:49:44
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 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;

void Test();

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;
}

void Test() {
    a.push_back(1);
    a.push_back(2);
    cout << a.front() << " " << a.back() << " ";
    a.pop_front();
    cout << a.front();
}