Cod sursa(job #2813417)

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

int arr[5000000];

int main() {
	int arr_length, seq_length;
	fin >> arr_length >> seq_length;
	for (int i = 0; i < arr_length; ++i) {
		fin >> arr[i];
	}
	deque<int> best_pos_min;
	long long sum = 0;
	for (int i = 0; i < arr_length; ++i) {
		while (best_pos_min.size() && arr[best_pos_min.back()] > arr[i]) {
			best_pos_min.pop_back();
		}
		best_pos_min.push_back(i);
		if (i >= seq_length - 1) {
			sum += arr[best_pos_min.front()];
		}
		if (best_pos_min.front() == i - seq_length + 1) {
			best_pos_min.pop_front();
		}
	}
	fout << sum;
	return 0;
}