Cod sursa(job #2675721)

Utilizator pctirziuTirziu Petre pctirziu Data 22 noiembrie 2020 13:30:04
Problema Secventa Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <deque>
#include <stack>
using namespace std;
#pragma GCC optimize("O2")
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;
    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;
    }
    for ( i=n; i>=1; i--)
    {
        int h = i;
        while( !st.empty() and v[st.top()] > v[i] )
        {
            h = dr[st.top()];
            st.pop();
        }
        if( st.empty() )
        {
            st.push( i );
            dr[i] = h;
        }
        else
        {
            dr[i] = h;
            st.push( i );
        }
        if( i == poz )
            break;
    }
    cout << st1[poz] << " " << dr[poz] << " " << maxx;
    return 0;
}