Cod sursa(job #1990458)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 11 iunie 2017 21:58:19
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#include <deque>

struct Pair {
    int first, second;
};

int main() {
    std::ifstream fileIn("deque.in");
    std::ofstream fileOut("deque.out");

    std::deque<Pair> myQ;

    int nV, nK;

    fileIn >> nV >> nK;

    long long sum(0);
    for (int i(1); i <= nV; i++) {
        Pair aux;
        aux.first = i;
        fileIn >> aux.second;

        while (!myQ.empty() && myQ.front().first <= i - nK) {
            myQ.pop_front();
        }

        while (!myQ.empty() && myQ.back().second >= aux.second) {
            myQ.pop_back();
        }

        myQ.push_back(aux);

        if (i >= nK) {
            sum += myQ.front().second;
        }
    }

    fileOut << sum;

    fileIn.close();
    fileOut.close();
    return 0;
}