Cod sursa(job #1175062)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 24 aprilie 2014 13:35:37
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.12 kb
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <cassert>
#include <climits>
#include <iomanip>

using namespace std;

const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long 
#define MOD1 666013
#define MOD2 666019
#define base 73

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;

#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end()) 
#define sz(a) int((a).size()) 
#define pb push_back 
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)



vi V, nex, val;
int N;

int search(int lf, int rg, int x);
void write(int pos);

//```
#define ONLINE_JUDGE

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#else 
	freopen("scmax.in", "r", stdin);
	freopen("scmax.out", "w", stdout);
#endif
	
	int i, pos, lgmax = 0;
	scanf("%d", &N);
	V.resize(N + 1);
	V[N] = INT_MAX;
	nex.resize(N);
	val.resize(N + 1);
	val[0] = N;

	for (i = 0; i < N; ++i) {
		scanf("%d", &V[i]);
	}

	for (i = N - 1; i >= 0; --i) {
		pos = search(0, lgmax, V[i]);
		nex[i] = val[pos];
		if (pos == lgmax) {
			++lgmax;
			val[lgmax] = i;
		}
		else {
			if (V[i] > V[val[pos + 1]]) {
				val[pos + 1] = i;
			}
		}
	}
	
	printf("%d\n", lgmax);
	write(val[lgmax]);
	
	return 0;
}

int search(int lf, int rg, int x) {
	if (lf >= rg) {
		if (x >= V[val[rg]]) {
			return rg - 1;
		}
		return rg;
	}
	else {
		int mid = (lf + rg) / 2;
		if (x >= V[val[mid]]) {
			return search(lf, mid - 1, x);
		}
		else {
			return search(mid + 1, rg, x);
		}
	}
}

void write(int pos) {
	printf("%d ", V[pos]);
	if (nex[pos] != N) {
		write(nex[pos]);
	}
}