Cod sursa(job #3211196)

Utilizator karo_8870Cazacu Christian Matei karo_8870 Data 8 martie 2024 18:34:46
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <deque>
#include <climits>

using namespace std;

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    long long N, K, x, max = INT_MIN, st;
    cin >> N >> K;

    deque<pair<int, int>> d;

    for (int i = 0; i < K; ++i) {
        cin >> x;

        while (!d.empty() && x < d.front().first) {
            d.pop_front();
        }

        d.push_front(make_pair(x, i));
    }

    for (int i = K; i <= N; ++i) {
        if (d.back().first > max) {
            max = d.back().first;
            st = i - K + 1;
        }

        if (d.back().second <= i - K) {
            d.pop_back();
        }

        cin >> x;

        while (!d.empty() && x < d.front().first) {
            d.pop_front();
        }

        d.push_front(make_pair(x, i));
    }

    cout << st << ' ' << st + K - 1 << ' ' << max;

    return 0;
}

/*
-7 9 2 4 -1 5 6 7 1

-7 2 4
*/