Cod sursa(job #3354849)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 20 mai 2026 23:32:18
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <bits/stdc++.h>

// 20.05 - habemus generatie noua de admini infoarena

using namespace std; 

string s;
deque <pair <int, int>> dq;
int pos;

int nextInt ()
{
    int sgn = 1, val = 0;
    while (s[pos] == ' ')
    {
        pos++;
    }
    while (s[pos] == '-')
    {
        sgn = -sgn;
        pos++;
    }
    while (isdigit(s[pos]))
    {
        val = val * 10 + (s[pos] - '0');
        pos++;
    }
    return sgn * val;
}

void solve ()
{
    int n, k;
    cin >> n >> k;
    cin.get();
    getline(cin, s);
    for (int i = 1; i <= k; i++)
    {
        int x = nextInt();
        while (!dq.empty() && dq.back().first >= x)
        {
            dq.pop_back();
        }
        dq.push_back({x, i});
    }
    int l = 1, r = k, ans = dq.front().first;
    for (int i = k + 1; i <= n; i++)
    {
        int x = nextInt();
        while (!dq.empty() && dq.back().first >= x)
        {
            dq.pop_back();
        }
        if (!dq.empty() && dq.front().second == i - k)
        {
            dq.pop_front();
        }
        dq.push_back({x, i});
        if (dq.front().first > ans)
        {
            ans = dq.front().first;
            l = i - k + 1;
            r = i;
        }
    }
    cout << l << " " << r << " " << ans << '\n';
}

signed main() 
{ 
#ifdef LOCAL 
    freopen("test.in", "r", stdin); 
    freopen("test.out", "w", stdout);
#else
    freopen("secventa.in", "r", stdin);
    freopen("secventa.out", "w", stdout);
#endif // LOCAL 
    ios_base::sync_with_stdio(false); 
    cin.tie(0); 
    cout.tie(0); 
    long long tt; 
    tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0; 
}