Cod sursa(job #2625809)

Utilizator marian222200Dimofte Marian marian222200 Data 6 iunie 2020 10:16:47
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
// tema_sd_mare.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include<fstream>
#include<stack>
#define N 5000013
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
int dq[N];
int v[N];
int main(){
    int n, k, i, ul = 0, pr = 1;
    long long suma = 0;
    fin >> n >> k;
    for (i = 1; i <= n; i++)fin >> v[i];

    for (i = 1; i <= n; i++) {
        while (pr <= ul && v[i] <= v[dq[ul]])ul--;//cautam la fiecare pas cel mai mic element, prin urmare le baza dq va fi minimul
        dq[++ul] = i;
        if (dq[pr] == i - k)
            pr++;
        if (i >= k)suma += v[dq[pr]];
    }
    fout << suma;
    return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project && select the .sln file