Cod sursa(job #2815440)

Utilizator george_buzasGeorge Buzas george_buzas Data 9 decembrie 2021 16:56:07
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <deque>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");

int main() {
	int n, seq_length;
	fin >> n >> seq_length;
	deque<pair <int, int>> best_pos_min;
	long long sum = 0;
	int current_value;
	for (int i = 0; i < n; ++i) {
		fin >> current_value;
		while (best_pos_min.size() && best_pos_min.back().second > current_value) {
			best_pos_min.pop_back();
		}
		best_pos_min.push_back(make_pair(i, current_value));
		if (i >= seq_length - 1) {
			sum += best_pos_min.front().second;
		}
		if (best_pos_min.front().first == i - seq_length + 1) {
			best_pos_min.pop_front();
		}
	}
	fout << sum;
	return 0;
}