Cod sursa(job #2957332)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 22 decembrie 2022 13:04:59
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <bits/stdc++.h>
using namespace std;

template <typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template <typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
using i64 = long long int;

const int INF = INT_MAX, MOD = 1e9 + 7;
const long long LINF = LLONG_MAX;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {0, 0, 0, -1, 1, -1, 1, 1, -1};
const int dy[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  /// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  ifstream cin("secventa.in");
  ofstream cout("secventa.out");

  int N, K; cin >> N >> K;
  vector<int> nums(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> nums[i];

  int start, max_base = -INF;
  deque<int> deq;
  for (int i = 1; i <= K; i++) {
    if (deq.empty()) {
      deq.push_back(i);
      continue;
    }

    while (not deq.empty() and nums[deq.back()] >= nums[i]) {
      deq.pop_back();
    }
    deq.push_back(i);
  }

  for (int i = K + 1; i <= N + 1; i++) {
    if (nums[deq.front()] > max_base) {
      max_base = nums[deq.front()];
      start = i - K;
    }
    if (i == N + 1) break;

    while (not deq.empty() and nums[deq.back()] >= nums[i]) {
      deq.pop_back();
    }
    deq.push_back(i);
    if (deq.front() == i - K)
      deq.pop_front();
  }

  cout << start << " " << start + K - 1 << " " << max_base << "\n";

  return 0;
}