Pagini recente » Cod sursa (job #1059269) | Cod sursa (job #956977) | Cod sursa (job #2102817) | Cod sursa (job #1669141) | Cod sursa (job #3148346)
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int NMAX = 1e5 + 2;
int a[NMAX] , prv[NMAX];
int dp[NMAX];
struct AIB {
pair<int , int> aib[NMAX];
int n;
AIB (int N)
{
n = N;
for (int i = 1; i <= n; i++)
{
aib[i] = {0 , 0};
}
}
void update (int pos , int val)
{
for (int i = pos; i <= n; i += (i & -i))
{
// aib[i] = max(aib[i] , val);
if (val > aib[i].first)
{
aib[i] = {val , pos};
}
}
}
pair<int , int> query (int pos)
{
pair<int , int> rez = {0 , 0};
for (int i = pos; i > 0; i -= (i & -i))
{
rez = max(aib[i] , rez);
}
return rez;
}
};
map<int , int> norm;
map<int , int> revNorm;
int main ()
{
freopen("scmax.in" , "r" , stdin);
freopen("scmax.out" , "w" , stdout);
int n; cin >> n;
vector<int> vals;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
norm[a[i]]++;
if (norm[a[i]] == 1)
{
vals.push_back(a[i]);
}
}
sort(vals.begin() , vals.end());
for (int i = 0; i < (int)vals.size(); i++)
{
norm[vals[i]] = i+1;
revNorm[i+1] = vals[i];
}
for (int i = 1; i <= n; i++)
{
a[i] = norm[a[i]];
}
AIB aib(n);
int ans = 0;
int crt = -1;
for (int i = 1; i <= n; i++)
{
pair<int , int> qry = aib.query(a[i] - 1);
dp[a[i]] = qry.first + 1;
prv[a[i]] = qry.second;
aib.update(a[i] , dp[a[i]]);
// ans = max(ans , dp[i]);
if (dp[a[i]] > ans)
{
ans = dp[a[i]];
crt = a[i];
}
}
vector<int> rez(ans);
for (int i = ans-1; i >= 0; i--)
{
rez[i] = revNorm[crt];
crt = prv[crt];
}
cout << ans << '\n';
for (int i = 0; i < ans; i++)
{
cout << rez[i] << ' ';
}
cout << '\n';
}