Cod sursa(job #2642783)

Utilizator alextmAlexandru Toma alextm Data 17 august 2020 11:03:49
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <deque>
using namespace std;

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;
	}
};

InParser fin("secventa.in");
ofstream fout("secventa.out");

#define MAXN 500000

int v[MAXN + 1];
deque<int> D;

int main() {
    int n, k, vmax, b, e;

    fin >> n >> k;
    for(int i = 1; i <= n; i++)
        fin >> v[i];

    vmax = -MAXN;
    b = e = 0;

    for(int i = 1; i <= n; i++) {
        while(!D.empty() && v[i] <= v[D.back()])
            D.pop_back();

        D.push_back(i);
        while(!D.empty() && i - k >= D.front())
            D.pop_front();

        if(i >= k && v[D.front()] > vmax) {
            vmax = v[D.front()];
            b = i - k + 1; e = i;
        }
    }

    fout << b << " " << e << " " << vmax << '\n';

    return 0;
}