Pagini recente » Cod sursa (job #303381) | Cod sursa (job #3209094) | Cod sursa (job #709216) | Cod sursa (job #2436004) | Cod sursa (job #2988059)
#include <bits/stdc++.h>
using namespace std;
/**
dp[i] = max(dp[j] + 1, dp[i]), j < i, a[j] < i
aib[] =
Query(a[i] - 1);
Update(a[i], dp[i]);
*/
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
int n;
int a[100005], c[100005];
pair <int ,int> b[100005];
pair <int, int> aib[100005];
int dp[100005];
int last[100005];
void Update(int poz, int val) {
for (int i = a[poz]; i <= n; i += (i & -i))
if (aib[i].first <= val) {
aib[i] = {val, poz};
}
}
int Query (int poz) {
int val = 0, poz_max = 0;
for (int i = poz; i > 0; i -= (i & -i))
if (val < aib[i].first) {
val = aib[i].first;
poz_max = aib[i].second;
}
return poz_max;
}
void Afis(int x) {
if (x != 0) {
Afis(last[x]);
fout << c[x] << " ";
}
}
int main()
{
fin >> n;
for (int i = 1; i <= n; i++)
fin >> a[i], b[i] = {a[i], i}, c[i] = a[i];
sort(b + 1, b + n + 1);
int val = 1;
a[b[1].second] = 1;
for (int i = 2; i <= n; i++) {
if (b[i].first != b[i - 1].first)
val++;
a[b[i].second] = val;
}
int ans = 1, poz_max = 0;
for (int i = 1; i <= n; i++) {
last[i] = Query(a[i] - 1);
dp[i] = max(dp[i], dp[last[i]] + 1);
Update(i, dp[i]);
if (ans < dp[i]) {
ans = dp[i];
poz_max = i;
}
}
fout << ans << "\n";
Afis(poz_max);
return 0;
}