Cod sursa(job #3324784)

Utilizator Luca_georgescuLuca Georgescu Luca_georgescu Data 23 noiembrie 2025 14:46:18
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

ifstream f("secventa.in");
ofstream g("secventa.out");

int n,k;

struct node
{
    int val, pos;
};

struct cmp
{
    bool operator()(node a, node b)
    {
        if ( a.val!=b.val ) return a.val>b.val;
        return a.pos>b.pos;
    }
};

priority_queue<node, vector<node>, cmp> pq;

const int nmax=5e5+5;
int a[nmax];

signed main()
{
    f >> n >> k;

    for (int i=1; i<=n; i++ )
        f >> a[i];

    int maxi=-INT_MAX, bestdr=0, bestst=0;
    for (int i=1; i<=n; i++ )
    {
        pq.push({a[i],i});

        if ( i>=k )
        {
            int st=i-k+1;

            while ( !pq.empty() && pq.top().pos<st )
                pq.pop();

            if ( pq.top().val>maxi )
            {
                maxi=pq.top().val;
                bestdr=i;
                bestst=st;
            }
        }
    }

    g << bestst << " " << bestdr << " " << maxi;
    return 0;
}