Cod sursa(job #2676322)

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

#include <iostream>
#include <fstream>

using namespace std;


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 poz_max = 0;
	int *maxLoc = new int[n];
	maxLoc[0] = 1;
	for (int i = 1;i < n;i++) {
		int max_sub = -1;
		int max_ind = -1;
		for (int j = 0; j < i; j++) {
			if (vec[j] > max_sub && vec[j] < vec[i]) {
				max_sub = vec[j];
				max_ind = j;
			}
			else
			{
				if (vec[j] == max_sub) {
					max_sub = maxLoc[max_ind] < maxLoc[j] ? vec[j] : max_sub;
					max_ind = maxLoc[max_ind] < maxLoc[j] ? j : max_ind;
				}
			}
		}
		if (max_ind == -1 ) {
			maxLoc[i] = 1;
		}
		else
		{
			maxLoc[i] = maxLoc[max_ind] + 1;
		}
		if (maxLoc[poz_max] < maxLoc[i]) {
			poz_max = i;
		}
	}
	
	ofstream output_file("scmax.out");
	output_file << maxLoc[poz_max] << "\n";
	int cnt = 1;
	int i = poz_max;
	cnt = maxLoc[poz_max];
	int* aux = new int[maxLoc[poz_max]];
	while (cnt > 0) {
		if (cnt == maxLoc[i]) {
			aux[cnt - 1] = vec[i];
			cnt--;
		}
		i--;
	}
	for (int i = 0;i < n;i++) {
		std::cout << maxLoc[i] << " ";
	}
	for (int i = 0;i < maxLoc[poz_max];i++) {
		output_file << aux[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