Cod sursa(job #2915649)

Utilizator AswVwsACamburu Luca AswVwsA Data 23 iulie 2022 20:00:22
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
//se incearca evitarea dequelui, cea mai tare structura de date...
#include <fstream>
using namespace std;

int st[500003], dr[500003], v[500003];
int sv[500003];
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;
	}
};
signed main()
{
    InParser cin("secventa.in");
    ofstream cout("secventa.out");
    int n, k, i, top = 0;
    cin >> n >> k;
    for (i = 1; i <= n; i++)
    {
        cin >> v[i];
        while (top and v[sv[top]] >= v[i])
            top--;
        st[i] = sv[top];
        sv[++top] = i;
    }
    top = 0;
    int ans = -30003;
    int a = 0;
    for (i = n; i >= 1; i--)
    {
        while (top and v[sv[top]] >= v[i])
            top--;
        if (sv[top] == 0)
            dr[i] = n + 1;
        else
            dr[i] = sv[top];
        sv[++top] = i;

        if (dr[i] - st[i] - 1 >= k)
        {
            if (ans < v[i])
            {
                ans = v[i];
                a = st[i] + 1;
            }
            else if (ans == v[i])
                a = min(a, st[i] + 1);
        }
    }
    cout << a << ' ' << a + k - 1 << " " << ans;
}