Cod sursa(job #1455393)

Utilizator GilgodRobert B Gilgod Data 27 iunie 2015 21:35:14
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
//pronuntat "deck" !? ayy lmao
#include <iostream>
#include <fstream>

#define mkpair(a,b) std::make_pair((a), (b))
#define tuple std::pair<int,int>

const char IN[] = "deque.in", OUT[] = "deque.out";
const int NMAX = 5000000;

using namespace std;

tuple deque[NMAX];
int head = -1, tail = -1;
int N, K;

inline void read_data() {
	freopen(IN, "r", stdin);
	scanf("%d %d", &N, &K);
	long long s = 0;
	for (int i = 0; i < N; ++i) {
		int x;
		scanf("%d", &x);
		while (tail != -1 && deque[tail].first >= x) --tail;
		while (head != -1 && i - deque[head].second >= K) ++head;
		if (head > tail) head = tail = -1;
		deque[++tail] = mkpair(x, i);
		if (head == -1) head = 0;
		if (i >= K - 1) {
			s += deque[head].first;
		}
	}
	fprintf(fopen(OUT, "w"), "%lld\n", s);
}

int main() {
	read_data();
	return 0;
}