Cod sursa(job #2755527)

Utilizator IanisBelu Ianis Ianis Data 27 mai 2021 16:21:02
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <stdio.h>
#include <ctype.h>
 
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
 
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
 
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};





using namespace std;
InParser f("secventa.in");
ofstream g("secventa.out");
int n, k, s;
int v[500001];
deque<int> dq;
int sf, Max = -1e5;
int main() {
	f >> n >> k;
	for (int i = 1; i <= n; i++) {
		f >> v[i];
		while (!dq.empty() && v[i] <= v[dq.back()])
			dq.pop_back();
		dq.push_back(i);

		if (dq.front() == i-k)
			dq.pop_front();

		if (i >= k) {
			if (Max < v[dq.front()]) {
				sf = i;
				Max = v[dq.front()];
			}
		}
	}
	g << sf - k + 1 << ' ' << sf << ' ' << Max;
	return 0;
}