Cod sursa(job #2929305)

Utilizator raresgherasaRares Gherasa raresgherasa Data 25 octombrie 2022 15:41:53
Problema Secventa Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("secventa.in");
ofstream fout ("secventa.out");

const int NM = 5e5 + 5;

int a[NM], n, k, mx, w, r;
deque<int>dq;

int main(){
  fin >> n >> k;
  for (int i = 1; i <= n; i++){
    fin >> a[i];
    while (!dq.empty() && a[i] <= a[dq.back()]){
      dq.pop_back();
    }
    dq.push_back(i);
    if (i >= k){
      int mn = a[dq.front()];
      if (mn > mx){
        mx = mn;
        w = dq.front();
        r = i;
      }
      if (i - dq.front() + 1 >= k){
        dq.pop_front();
      }
    }
  }
  for (int i = w - (k - (r - w)) + 1; i < w; i++){
    fout << a[i] << " ";
  }
  for (int i = w; i <= r; i++){
    fout << a[i] << " ";
  }
}