Cod sursa(job #2264524)

Utilizator TyFrostbyteIon Robert-Gabriel TyFrostbyte Data 20 octombrie 2018 10:17:07
Problema Deque Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <deque>
 
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");

struct fucker {
	long long first;
	int second;

	fucker(long long v, int i) : first(v), second(i) {}
};

deque< fucker > 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.emplace_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.emplace_back( x,i );
 
		sum += DQ.front().first;
	}
	fout << sum << '\n';
 
	return 0;
}