Cod sursa(job #3133447)

Utilizator daristyleBejan Darius-Ramon daristyle Data 25 mai 2023 18:10:38
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

const int N_MAX = 1e6;
const int X_MAX = 2e9;
int v[N_MAX + 1], sclen[N_MAX + 1], last[N_MAX + 1];

int binary_search(int x, int last[], int n) {
	int begin = 0, end = n, mid; //[begin;end)
	while(end - begin > 1){
		mid = ((begin + end) >> 1);
		if(x >= last[mid])
			end = mid;
		else
			begin = mid;
	}

	return begin;
}

int main() {
	int n;
	fin >> n;
	for(int i = 0; i < n; ++i)
		fin >> v[i];

	v[n] = sclen[n] = 0;
	last[0] = X_MAX + 1;
	int max = 0;
	for(int i = n - 1; i >= 0; --i){
		int pos_max = binary_search(v[i], last, max + 1);
		sclen[i] = pos_max + 1;
		if(v[i] > last[sclen[i]])
			last[sclen[i]] = v[i];
		if(sclen[i] > max)
			max = sclen[i];
	}

	fout << max << '\n';

	int pos_max = n;
	for(int i = 0; i < n; ++i)
		if(sclen[pos_max] < sclen[i])
			pos_max = i;

	int i = pos_max, nxt;
	while(sclen[i]){
		fout << v[i] << ' ';

		nxt = i + 1;
		while(sclen[nxt] != sclen[i] - 1)
			++nxt;

		i = nxt;
	}
	fout.put('\n');

	fin.close();
	fout.close();
	return 0;
}