Cod sursa(job #1455380)

Utilizator GilgodRobert B Gilgod Data 27 iunie 2015 21:03:14
Problema Secventa Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <assert.h>
#include <list>

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

const char IN[] = "secventa.in", OUT[] = "secventa.out";
const int NMAX = 500000;
const int INF = 0x3f3f3f3f;



using namespace std;

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

inline void read_data() {
	assert(freopen(IN, "r", stdin));
	assert(scanf("%d %d\n", &N, &K));
	int best = -INF, best_start = -1, best_end = -1;
	for (int i = 0; i < N; ++i) {
		int x = 0;
		int sgn = 0;
		char c;
		scanf("%c", &c);
		do {
			if (c == '-') sgn = 1;
			else x = (x << 3) + (x << 1) + (c - '0');
			int r;
			if((r =scanf("%c", &c)) == EOF || r == 0) break;
		} while (c >= '0' && c <= '9');
		if (sgn) x = -x;
		while (tail != -1 && deq[tail].first >= x) {
			--tail;
		}
		while (head != -1 && i - deq[head].second >= K) {
			++head;
		}
		if (head > tail) head = tail = -1;
		deq[++tail] = mkpair(x, i);
		if (head == -1) head = 0;
		if (i >= K - 1) {
			if (deq[head].first > best) {
				best = deq[head].first;
				best_start = i - K + 1;
				best_end = i;
			}
		}
	}
	fprintf(fopen(OUT, "w"), "%d %d %d\n", best_start + 1, best_end + 1, best);
	fclose(stdin);
}

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