Cod sursa(job #2850701)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 17 februarie 2022 13:20:02
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <fstream>
#include <queue>
#include <stdio.h>
#include <ctype.h>
#define INF 30005
#define NMAX 500005

using namespace std;

/****************************/
/// PARSARE

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;
	}
};
/****************************/
/// INPUT / OUTPUT

InParser fin("secventa.in");
ofstream g("secventa.out");
/****************************/
/// GLOBAL DECLARATIONS

int N, K;
int num, ans = -INF, l, r;
int v[NMAX];

pair <int, int> p;
deque <int> dq;
/****************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/****************************/
////------------------------------------
inline void ReadInput()
{
    fin >> N >> K;
}
////------------------------------------
inline void Solution()
{
    for (int i = 1 ; i <= K ; ++ i)
    {
        fin >> v[i];

        while (!dq.empty() && v[dq.back()] >= v[i]) dq.pop_back();
        dq.push_back(i);
    }
    ans = v[dq.front()];
    l = 1, r = K;
    for (int i = K + 1 ; i <= N ; ++ i)
    {
        fin >> v[i];
        while (!dq.empty() && v[dq.back()] >= v[i]) dq.pop_back();
        dq.push_back(i);

        if (dq.front() <= i - K) dq.pop_front();
        if (ans < v[dq.front()])
        {
            ans = v[dq.front()];
            l = i - K + 1, r = i;
        }
    }
}
////------------------------------------
inline void Output()
{
    g << l << " " << r << " " << ans;
}
////------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}