Cod sursa(job #1697907)

Utilizator quochuyaNguyen Quoc Huy quochuya Data 3 mai 2016 10:09:31
Problema Secventa Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

const int maxN = (int)5E5;

int n, k, l, r, m, top, bot;
int a[maxN], dq[maxN];

int main() {
	freopen("secventa.in", "r", stdin);
	freopen("secventa.out", "w", stdout);
	scanf("%d %d", &n, &k);
	for(int i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	m = -(int)2E9;

	top = -1; bot = 0;
	for(int i = 0; i < n; ++i) {
		while (top >= bot && a[i] <= a[dq[top]]) --top;
		dq[++top] = i;
		while (top >= bot && dq[bot] < i-k+1) ++bot;
		if (i >= k-1 && a[dq[bot]] > m) {
			m = a[dq[bot]];
			l = i-k+1;
			r = i;
		}
	}

	printf("%d %d %d\n", l+1, r+1, m);
	return 0;
}