Cod sursa(job #2684790)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 14 decembrie 2020 20:47:05
Problema Secventa Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("secventa.in");
ofstream fout("secventa.out");

int main() {
    int N, K;
    fin >> N >> K;
    deque<pair<int,int>> Q;
    for(int i = 1; i <= K; ++i) {
        int x;
        fin >> x;
        while(!Q.empty() && Q.back().first > x)
            Q.pop_back();
        Q.emplace_back(x, i);
    }
    int st = 1, dr = K, ans = Q.front().first;
    for(int i = K + 1; i <= N; ++i) {
        int x;
        fin >> x;
        if(!Q.empty() && Q.front().second == i - K)
            Q.pop_front();
        while(!Q.empty() && Q.back().first > x)
            Q.pop_back();
        Q.emplace_back(x, i);
        if(Q.front().first > ans) {
            ans = Q.front().first;
            st = Q.front().second;
            dr = i;
        }
    }
    fout << st << ' ' << dr << ' ' << ans;
}