Cod sursa(job #2264513)

Utilizator TyFrostbyteIon Robert-Gabriel TyFrostbyte Data 20 octombrie 2018 10:11:51
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <deque>
#include <algorithm> 

using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
deque< pair<long long, int> > DQ;

int main() {
	int n, k;
	fin >> n >> k;

	long long x;

	for(int i = 0; i < k - 1; i++)
	{
		fin >> x;
		while (!DQ.empty() && DQ.back().first > x)
			DQ.pop_back();
		DQ.push_back({x, i});
	}

	long long sum = 0;
	
	for (int i = k - 1; i < n; i++) {
		// while (i - DQ.front().second + 1 > k) DQ.pop_front();
		while (DQ.front().second < i - k + 1) DQ.pop_front();
		
		fin >> x;

		while (!DQ.empty() && DQ.back().first > x)
			DQ.pop_back();
		DQ.push_back({ x,i });

		sum += DQ.front().first;
}