Cod sursa(job #2611368)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 6 mai 2020 19:14:29
Problema Secventa Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <stdio.h>
#include <ctype.h>
#define Flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
ofstream out("secventa.out");
int n, k;
int a[500010];
int mini=2000000000;
int xx, yy;

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;
	}
};
struct dedeque
{
    deque <int> dq;
    dedeque ()
    {
        dq.resize(0);
    }
    int front()
    {
        return dq.front();
    }
    void push(int val)
    {
        while(!dq.empty()&&a[dq.back()]>a[val])
            dq.pop_back();
        dq.push_back(val);
    }
    void pop(int poz)
    {
        if(dq.front()==poz)
            dq.pop_front();
    }
};
int main()
{
    InParser in("secventa.in");
    in>>n>>k;
    for(int i=1; i<=n; i++)
        in>>a[i];
    dedeque q;
    for(int i=1; i<=k; i++)
        q.push(i);

    xx=1; yy=k;
    mini=a[q.front()];
    for(int i=k+1; i<=n; i++)
    {
        q.push(i);
        q.pop(i-k);
        //cout<<q.front()<<"\n";
        if(mini<a[q.front()])
        {
            mini=a[q.front()];
            xx=i-k+1;
            yy=i;
        }
    }
    out<<xx<<" "<<yy<<" "<<mini;
    return 0;
}