Cod sursa(job #2758107)

Utilizator mihnea_buzoiuMihnea Buzoiu mihnea_buzoiu Data 8 iunie 2021 17:31:05
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
//
//  deque.cpp
//  probleme
//
//  Created by Mihnea Buzoiu on 6/2/21.
//

#include <stdio.h>
#include <iostream>
#include <queue>

using namespace std;

int v[5000001];
deque <int> q;

int main(int argc, const char * argv[]) {
    
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    int n, k;
    scanf("%d %d", &n, &k);
   
    long long smin = 0;
    for (int i=0; i<n; i++)
    {
        if (!q.empty() && q.front() <= i - k)
            q.pop_front();
                
        scanf("%d", &v[i]);
        
        while (!q.empty() && v[q.back()] > v[i])
            q.pop_back();
        
        q.push_back(i);
            
        if (i >= k - 1)
            smin += v[q.front()];
    }
    
    printf("%lld", smin);
}

/*
9 3
-7
9
2
4
-1
5
6
7
1
*/