Cod sursa(job #2917530)

Utilizator AlexandruBenescuAlexandru Benescu AlexandruBenescu Data 5 august 2022 16:11:14
Problema Secventa Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
#define L 500005
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");

struct MS{
  int val;
  int ind;
};
MS v[L], deq[L];

int main(){
  int n, k, i, j1, j2, mx, l, r;
  fin >> n >> k;
  for (i = 1; i <= n; i++){
    fin >> v[i].val;
    v[i].ind = i;
  }
  mx = l = r = -L;
  j1 = j2 = 0;
  for (i = 1; i <= n; i++){
    if (j2 && deq[j1].ind + k <= i)
      j1++;
    while (j2 && deq[j2 - 1].val > v[i].val)
      j2--;
    deq[j2++] = v[i];
    if (i >= k && mx < deq[j1].val){
      mx = deq[j1].val;
      l = i - k + 1;
      r = i;
    }
  }
  fout << l << " " << r << " " << mx << "\n";
  return 0;
}