Cod sursa(job #2698891)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 23 ianuarie 2021 11:09:17
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.25 kb
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
 
#define fisier 1
 
using namespace std;
 
typedef long long ll;
 
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI 
const double eps = 1e-9;
 
class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;
 
	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}
 
public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	
	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
	
	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
}f("scmax.in");
ofstream g("scmax.out");
 
int n, v[100002], vs[100002], ans[100002];
int mp[100002];
 
int aib[100002];

int vans[100002];
inline void upd(int poz, int val)
{
	for(; poz <= n; poz += (poz & (-poz)))
		aib[poz] = max(aib[poz], val);
}
inline int query(int poz)
{
	int ans = 0;
	for(; poz; poz -= (poz & (-poz)))
		ans = max(ans, aib[poz]);
	return ans;
}
bool cmp(int a, int b)
{
	return v[a] < v[b];
}
int main()
{
	
	f >> n;
	for(int i = 1; i <= n; ++i)
		f >> v[i], vs[i] = i;
	sort(vs+1, vs+n+1, cmp);
	int cnt = 0;
	for(int i = 1; i <= n; ++i)
	{
		if(v[vs[i]] > v[vs[i-1]])
			++cnt;
		mp[vs[i]] = cnt;
	}
	int mx_ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		int val = mp[i];
		ans[i] = query(val - 1) + 1;
		upd(val, ans[i]);
		mx_ans = max(mx_ans, ans[i]);
	}
	g << mx_ans << '\n';
	int sz = mx_ans;
	int prv = 2000000001;
	for(int i = n; i >= 1; --i)
		if(ans[i] == mx_ans && v[i] < prv)
			vans[mx_ans] = v[i], --mx_ans, prv = v[i];
	for(int i = 1; i <= sz; ++i)
		g << vans[i] << " ";
	return 0;
}