Cod sursa(job #3032750)

Utilizator pctirziuTirziu Petre pctirziu Data 22 martie 2023 17:49:33
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <deque>
#include <stack>
using namespace std;
#pragma GCC optimize("O3")
ifstream cin("secventa.in");
ofstream cout("secventa.out");
deque < int > dq;
stack < int > st;
const int NMAX=500005;
int v[NMAX], st1[NMAX], dr[NMAX];
int main()
{
    cin.tie(NULL);
    cout.tie(NULL);
    ios::sync_with_stdio(false);
    int n, i, j, k, maxx=-300005, poz = 0 ,poz1;
    cin >> n >> k;
    for( i=1; i<=n; i++ )
    {
        cin >> v[i];
        while( !dq.empty() and v[dq.back()] > v[i] )
            dq.pop_back();
        dq.push_back( i );
        if( dq.front() <= i - k )
            dq.pop_front();
        if( i>=k )
            if( v[dq.front()] > maxx )
            {
                maxx = v[dq.front()];
                poz = dq.front();
            }
    }
    for ( i=1; i<=n; i++)
    {
        int h = i;
        while( !st.empty() and v[st.top()] > v[i] )
        {
            h = st1[st.top()];
            st.pop();
        }
        if( st.empty() )
        {
            st.push( i );
            st1[i] = h;
        }
        else
        {
            st1[i] = h;
            st.push( i );
        }
        if( i == poz )
            break;
    }
    if( poz - st1[poz] >= k )
        cout << st1[poz] << " " << poz << " " << maxx;
    else
        cout << st1[poz] << " " << st1[poz] + k -1 << " " << maxx;
    return 0;
}