Cod sursa(job #2491521)

Utilizator Vlad.Vlad Cristian Vlad. Data 12 noiembrie 2019 18:24:59
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");
int n, k, currentNr;
deque<pair<int, int>> deq;
int bazaMaxi;
int pSf;
int main()
{
    fin >> n >> k;
    bazaMaxi = -2147483648;
    for (int i = 0; i < n; ++i) {
        fin >> currentNr;
        while (!deq.empty() && currentNr <= deq.back().first) {
            deq.pop_back();
        }
        deq.push_back(make_pair(currentNr, i));
        if (i - deq.front().second == k) {
            deq.pop_front();
        }
        if (i >= k - 1 && deq.front().first > bazaMaxi)
        {
            bazaMaxi = deq.front().first;
            pSf = i;
        }
    }
    fout << pSf - k + 2 << " " << pSf + 1 << " " << bazaMaxi << "\n";
    return 0;

}