Pagini recente » Cod sursa (job #1612646) | Cod sursa (job #2778983) | Cod sursa (job #2208185) | Cod sursa (job #1540867) | Cod sursa (job #1701652)
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
#define NMax 100005
int n, res[NMax], v[NMax], lst[NMax], d[NMax], aib[NMax], bst, up[NMax];
void update(int x, int ind)
{
for (; x <= n; x += x ^ (x-1)&x)
if (d[ind] > d[aib[x]])
aib[x] = ind;
}
int query(int x)
{
int mx = 0;
for (; x; x -= x ^ (x - 1)&x)
mx = aib[x];
return mx;
}
int main(void)
{
int i, h = 1;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
cin >> n;
for (i = 1; i <= n; ++i)
{
cin >> v[i];
res[i] = lst[i] = v[i];
}
sort(lst + 1, lst + n + 1);
for (i = 2; i <= n;++i)
if (lst[i] != lst[h])
lst[++h] = lst[i];
for (i = 1; i <= n; ++i)
v[i] = lower_bound(lst + 1, lst + h + 1, v[i]) - lst;
for (i = 1; i <= n; ++i)
{
up[i] = query(v[i] - 1);
d[i] = d[up[i]] + 1;
update(v[i], i);
}
for (i = 1; i <= n;++i)
if (d[bst] < d[i])
bst = i;
cout << d[bst];
for (h = 0, i = bst; i; i = up[i])
lst[++h] = res[i];
for (i = h; i; --i)
cout << lst[i];
cout << endl;
fin.close();
fout.close();
system("pause");
return 0;
}