Cod sursa(job #40319)

Utilizator ZuziFilip Sanziana Zuzi Data 27 martie 2007 12:48:03
Problema Secventa Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <stdio.h>
#define NMAX 500001
#define MIN -30000

int n, k, x[NMAX], poz[NMAX], q[NMAX];
int max, msf, l;

void read()
{
	int i;
	scanf("%d%d", &n, &k);
	for (i = 1; i <= n; i++)
		scanf("%d", &x[i]);
}

void rezolv()
{
	int i, beg, end, j;
	beg = end = 1; q[1] = x[1]; poz[1] = 1;
	max = MIN;

	for (i = 1; i <= n; i++)
	{
		if (poz[beg] + k <= i)
			beg++;
		for (j = end; x[i] < q[j] && j >= beg; j--);
		end = j; 
		q[++end] = x[i], poz[end] = i;
		if (i >= k && q[beg] > max)
			max = q[beg], msf = i;
	}	
}

void afisare()
{
	printf("%d %d %d\n", msf - k + 1, msf, max);
}

int main()
{
	freopen("secventa.in", "r", stdin);
	freopen("secventa.out", "w", stdout);
	read();
	rezolv();
	afisare();
	fcloseall();
	return 0;
}