Cod sursa(job #983161)

Utilizator danlexDan Alexandru danlex Data 10 august 2013 23:31:15
Problema Secventa Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
#define MAX  500002

int k, n, v[MAX], xbase = -1000000, xmin, xmax;
deque<int> dq;

void print(){
	cout << endl;
	cout << n << " " << k << endl;
	for(int i = 0; i < n; i ++){
		cout << v[i] << " "; 
	}
	cout << endl;
	cout << xmin+1 << " " << xmax+1 << " " << xbase << endl;
	cout << endl;
}

void read(){
    ifstream fi("secventa.in");
    fi >> n;
	fi >> k;
	for (int i = 0; i < n; i ++){
		fi >> v[i];
	}
    fi.close();
}

void write(){
    ofstream fo("secventa.out");
    fo << xmin+1 << " " << xmax+1 << " " << xbase;
    fo.close();

}

void compute(){
	for(int i = 1; i <= n; i ++){
		while(!dq.empty() && dq.front() < i - k + 1){
			dq.pop_front();
		}
		while(!dq.empty() && v[i] < v[dq.back()]){
			dq.pop_back();
		}

		dq.push_back(i);

		if(i > k - 1 && v[dq.front()] > xbase){
			xbase = v[dq.front()];
			xmin = i - k + 1;
			xmax = i;
		}
	} 
	print();
}

int main(void){
    read();
    compute();
	write();
    return 0;
}