Cod sursa(job #1048473)

Utilizator dm1sevenDan Marius dm1seven Data 5 decembrie 2013 21:53:15
Problema Secventa Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <limits>
#include <algorithm>
#include <time.h>
#include <stdlib.h>

using namespace std;

void generate_sequence(int N, int K, int MAX_VAL, string file)
{
	srand((unsigned int) time(0));
	ofstream ofs(file);
	ofs << N << " " << K << endl;
	for (int i = 1; i <= N; i++) ofs << rand() % MAX_VAL << " ";
	ofs.close();
}

//int pb_014_secventa()
int main()
{
	string in_file = "secventa.in";
	string out_file = "secventa.out";

	//generate_sequence(50000, 10000, 30000, in_file);

	clock_t start_time = clock();

	const int MAX_N = 50000;
	int N, K;
	
	int x[MAX_N + 1];

	ifstream ifs(in_file);
	ifs >> N >> K;
	//read the array
	for (int i = 1; i <= N; i++) ifs >> x[i];
	ifs.close();	

	int deq[MAX_N + 1];
	//deq[1] = x[1];
	int Fd = 1, Ld = 0;
	int id_mm = 1, max_min = numeric_limits<int>::min();
	for (int i = 1; i <= N; i++)
	{
		//insert one element in the proper location in the deq		
		while (Ld - Fd >= 0 && x[i] < deq[Ld]) Ld--;
		deq[++Ld] = x[i];
		
		if (i >= K)
		{
			int cmin = deq[Fd];
			if (cmin > max_min) { max_min = cmin; id_mm = i; }
			if (x[i - K + 1] == deq[Fd]) Fd++; //pop the first element					
		}		
	}

	ofstream ofs(out_file);
	ofs << id_mm - K + 1 << " " << id_mm << " " << max_min;
	ofs.close();

	clock_t end_time = clock();

	cout << (double) (end_time - start_time) / CLOCKS_PER_SEC << endl;

	return 0;
}