Cod sursa(job #2896771)

Utilizator Horia_haivasHaivas Horia Horia_haivas Data 30 aprilie 2022 15:47:54
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
/*
    "TLE is like the wind, always by my side"
    - Yasuo - 2022 -
*/
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "

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");

deque <int> dq;
int v[500001];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,k,i,mx,is;
    fin >> n >> k;
    mx=-100000;
    for (i=1; i<=n; i++)
    {
        fin >> v[i];
        while (dq.size() && v[i]<=v[dq.back()])
            dq.pop_back();
        if (dq.front()<=i-k && dq.size())
            dq.pop_front();
        dq.push_back(i);
        if (i>=k)
        {
            if (v[dq.front()]>mx)
            {
                mx=v[dq.front()];
                is=i;
            }
        }
    }
    fout << is-k+1 << " " << is << " " << mx;
}