Cod sursa(job #1759990)

Utilizator andreioneaAndrei Onea andreionea Data 20 septembrie 2016 05:03:20
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#include <deque>
#include <vector>

int main()
{
	using ValuePair = std::pair<int, int>;
	std::deque<ValuePair> q;
	std::vector<int> v;
	int n, k;
	std::ifstream fin("deque.in");
	fin >> n >> k;
	int x;
	long long result = 0ll;
	for (int i = 1; i <= n; ++i)
	{
		fin >> x;
		while (!q.empty() && q.back().first > x)
		{
			q.pop_back();
		}
		q.push_back({x, i});
		if (q.front().second == i - k)
		{
			q.pop_front();
		}
		if (i >= k){
			result += q.front().first;
		}
	}
	fin.close();
	std::ofstream fout("deque.out");
	fout << result;
	fout.close();
	return 0;
}