Cod sursa(job #3193565)

Utilizator CraiuAndreiCraiu Andrei David CraiuAndrei Data 14 ianuarie 2024 22:07:22
Problema Secventa Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <deque>
using namespace std;
ifstream fin("secventa.in");
ofstream fout("secventa.out");

int n, k, a[500005], m;
deque<int> q;

int main()
{
    int i, st=1;
    fin >> n >> k;
    for (i = 1; i <= n; i++)
        fin >> a[i];
    for (i = 1; i <= k; i++)
    {
        while (!q.empty() && a[i] < a[q.back()])q.pop_back();
        q.push_back(i);
        if (a[i] < m)m = a[i];
    }
    for (i = k+1; i <= n; i++)
    {
        while (!q.empty() && a[q.back()] >= a[i])q.pop_back();
        while(!q.empty() && i - q.front() >= k)q.pop_front();
        q.push_back(i);
        if (a[q.front()] > m) 
        {
            m = a[q.front()]; 
            st = i; 
        }
    }
    fout << st - k + 1 << " " << st << " " << m;
    return 0;
}