Cod sursa(job #2676294)

Utilizator AlexandraChiorean28Alexandra Chiorean AlexandraChiorean28 Data 23 noiembrie 2020 22:00:22
Problema Subsir crescator maximal Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.31 kb
// SCmax.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>

using namespace std;

void read_input(int n, int** vec) {
	ifstream file("scmax.in");
	file >> n;
	int value = 0;
	*vec = (int*) malloc (n * sizeof(int));
	for (int i = 0; i < n;i++) {
		file >> value;
		*vec[i] = value;
	}
}

int* maxSeq(int n, int vec[], int* maxL) {
	int indStart = 0;
	int indEnd = 0;
	int crrStart = -1;
	int crrEnd = -1;
	int maxLoc = -1;
	for (int i = 1; i < n; i++)
	{
		if (vec[i - 1] < vec[i]) 
		{
			if (crrStart == -1)
			{
				crrStart = i - 1;
			}
			crrEnd = i;
		}
		else
		{
			if (maxLoc < crrEnd - crrStart + 1) 
			{
				maxLoc = crrEnd - crrStart + 1;
				indStart = crrStart;
				indEnd = crrEnd;
				crrStart = -1;
				crrEnd = -1;
			}
		}
	}
	//orderd array
	if (sizeof(vec) / sizeof(vec[0]) == 1) {
		*maxL = 1;
		return vec;
	}
	if (maxLoc < crrEnd - crrStart + 1)
	{
		maxLoc = crrEnd - crrStart + 1;
		indStart = crrStart;
		indEnd = crrEnd;
	}
	//*seq = (int*)malloc(n * sizeof(int));
	int *seq = new int[maxLoc];
	int j = -1;
	for (int i = indStart; i <= indEnd; i++) {
		seq[++j] = vec[i];
	}
	*maxL = maxLoc;
	return seq;

}

int main() {
	int n = 0; 
	int* vec = NULL;
	ifstream file("scmax.in");
	file >> n;
	int value = 0;
	vec = (int*)malloc(n * sizeof(int));
	for (int i = 0; i < n;i++) {
		file >> value;
		vec[i] = value;
	}
	file.close();
	int max = 0;
	int *seq = maxSeq(n, vec, &max);
	ofstream output_file("scmax.out");
	output_file << max << "\n";
	for (int i = 0; i < max;i++) {
		output_file << seq[i]<< " ";
	}
	return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file