Cod sursa(job #2629711)

Utilizator gasparrobert95Gaspar Robert Andrei gasparrobert95 Data 22 iunie 2020 13:53:34
Problema Deque Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
deque <int> q;
int n, k, el;
long long sum;

int main() {
    fin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        fin >> el;
        if (k == 1) {
            sum += el;
            continue;
        }
        if (i == 1) {
            q.push_back(el);
            continue;
        }
        int lmax = q.size() - 1;
        while (lmax >= 0) {
            if (q[lmax] > el) {
                q.erase(q.begin() + lmax);
                q.push_back(el);
                --lmax;
            }
            else
                break;
        }
        q.push_back(el);
        if (i >= k && !q.empty()) {
            sum += q.front();
            q.pop_front();
        }
        for (int k = 0; k < q.size(); ++k)
            cout << q[k] << " ";
        cout << "\n";
    }
    fout << sum;
    return 0;
}