Cod sursa(job #2795213)

Utilizator lolismekAlex Jerpelea lolismek Data 6 noiembrie 2021 09:55:17
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

ofstream fout("secventa.out");

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};


const int INFINIT = 30001;
const int N = 500001;
int v[N];
deque <int> D;

int main()
{
    InParser fin("secventa.in");
    int n, k, best = -INFINIT, st, dr, i;
    fin >> n >> k;
    for(i = 1; i <= n; i++){
        fin >> v[i];
        while(!D.empty() && v[i] <= v[D.back()])
            D.pop_back();
        D.push_back(i);
        if(D.front() == i - k)
            D.pop_front();
        if(v[D.front()] > best && i >= k){
            best = v[D.front()];
            dr = i;
            st = i - k + 1;
        }
    }
    fout << st << " " << dr << " " << best;
    return 0;
}