Cod sursa(job #2883106)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 1 aprilie 2022 10:27:59
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>

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

const int N = 5e5 + 5;

int v[N];

int main() {
  InParser cin("secventa.in");
  ofstream cout("secventa.out");
  int n, k, ans = -1e9, l, r;
  cin >> n >> k;
  deque<int> dq;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    while (!dq.empty() && dq.front() < i - k + 1)
      dq.pop_front();
    while (!dq.empty() && v[i] <= v[dq.back()])
      dq.pop_back();
    dq.push_back(i);
    if (i >= k - 1 &&  v[dq.front()] > ans) {
      ans = v[dq.front()];
      l = i - k + 1;
      r = i;
    }
  }
  cout << l + 1 << " " << r + 1 << " " << ans << "\n";
  cout.close();
  return 0;
}