Pagini recente » Cod sursa (job #1138382) | Cod sursa (job #1152334) | Cod sursa (job #200132) | Cod sursa (job #2951007) | Cod sursa (job #2492491)
#include <fstream>
#include <algorithm>
#define NMax 100005
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int N, Res[NMax], v[NMax], lst[NMax], D[NMax], aib[NMax], bst, up[NMax];
void Update(int poz, int x)
{
while(poz <= N)
{
if(D[x] > D[aib[poz]])
aib[poz] = x;
poz += poz & (-poz);
}
}
int Query(int poz)
{
int s = 0;
while(poz > 0)
{
if(D[aib[poz]] > D[s])
s += aib[poz];
poz -= poz & (-poz);
}
return s;
}
int main()
{
int i, h = 1;
fin >> N;
for (i = 1; i <= N; ++i)
{
fin >> 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;
fout << D[bst] << "\n";
for(h = 0, i = bst; i; i = up[i])
lst[++h] = Res[i];
for(i = h; i; --i)
fout << lst[i] <<" ";
fin.close();
fout.close();
return 0;
}