Cod sursa(job #2748791)

Utilizator bogdanvladmihaiBogdan Vlad-Mihai bogdanvladmihai Data 3 mai 2021 13:45:36
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>

#define deb(x) << "[" << #x << " = " << x << "]"

using namespace std;

//ifstream in("secventa.in");
ofstream out("secventa.out");

using ll = long long;

const int inf = (int)3e4 + 5;
const int maxN = (int)5e5 + 5;

int n, k;

int v[maxN];
	
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 in("secventa.in");

int main() {
  in >> n >> k;
  deque<int> dq;
  int ans = -inf;
  pair<int, int> pos;
  for (int i = 1; i <= n; i++) {
    in >> v[i];
    while ((int)dq.size() > 0 && v[dq.back()] >= v[i]) {
      dq.pop_back();
    }
    dq.push_back(i);
    if (i >= k) {
      if (v[dq.front()] > ans) {
        ans = v[dq.front()];
        pos = make_pair(i - k + 1, i);
      }
    }
    if (dq.front() <= i - k + 1) {
      dq.pop_front();
    }
  }
  out << pos.first << " " << pos.second << " " << ans << "\n";
  return 0;
}