Cod sursa(job #2029098)

Utilizator Horea_Mihai_SilaghiHorea Mihai Silaghi Horea_Mihai_Silaghi Data 29 septembrie 2017 11:46:52
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <deque>

using namespace std;

deque <pair <int, int>> coada;
int v[5000001];

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

int main()
{
    int n, k, cnt = 0,maxi,c,j,st,dr,q;
    cin>>n>>k;
    for ( int i = 1; i <= n; ++i )
        cin>>v[i];
    maxi=v[1];
    for ( int i = 1; i <= n; ++i )
    {
        while ( coada.empty() == 0 && coada.back().first > v[i])
            coada.pop_back();
        while ( coada.front().second <= i-k && coada.empty() == 0 )
            coada.pop_front();
        coada.push_back({v[i], i});
        if ( i >= k )
            if(maxi<coada.front().first)
            {
                q=coada.front().second;
                maxi=coada.front().first;
                j=q-1;
                c=1;
                while(v[q]<v[j]&&c<k)
                {
                    c++;
                    j--;
                }
                st=j+1;
                dr=st+k-1;

            }
    }
    cout<<st<<" "<<dr<<" "<<maxi;
}