Cod sursa(job #3151548)

Utilizator andreea678Rusu Andreea andreea678 Data 21 septembrie 2023 19:00:05
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <vector>
#include <algorithm>
using namespace std;
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;

    }

};
InParser fin("secventa.in");

ofstream fout("secventa.out");
int main()
{
    deque<pair<int, int>> d;
    int n, a, k, ind=0;
    int maxim=-999999;
    fin >> n >> k;
    for (int i=1; i<=n; ++i) {
        fin >> a;
        // -1 2 3 1 0 4 8 6
        while(!d.empty() && d.back().first>a) {
            d.pop_back();
        }
        d.emplace_back(a,i);
        if (!d.empty() && d.front().second==i-k) {
            //cout << d.front().first << ' ' << d.front().second << '\n';
            d.pop_front();
        }
        //cout << i << ' ' << d.front().first << '\n';
        if (!d.empty() && i>=k) {
            if(maxim<d.front().first) {
                maxim=d.front().first;
                ind=i;
            }
        }
    }
    fout << ind-k+1<< ' ' << ind << ' ' << maxim;
    return 0;
}