Pagini recente » Monitorul de evaluare | Cod sursa (job #2780192) | Cod sursa (job #171680) | Cod sursa (job #882564) | Cod sursa (job #2445945)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
struct scmax{
int poz, val;
}v[100005];
int aib[100005], n, d[100005], prec[100005], init[100005];
stack <int> st;
void read(){
fin >> n;
for (int i = 1; i <= n; i++){
fin >> init[i];
v[i].val = init[i];
v[i].poz = i;
}
}
int compare(scmax x, scmax y){
if (x.val == y.val)
return x.poz > y.poz;
return x.val < y.val;
}
void update(int poz){
for (int i = poz; i <= n; i += i & (-i))
if (d[poz] > d[aib[i]])
aib[i] = poz;
}
int cauta(int poz){
int sol = 0;
for (int i = poz; i > 0; i -= i & (-i))
if (d[aib[i]] > d[sol])
sol = aib[i];
return sol;
}
void solve(){
sort(v + 1, v + n + 1, compare);
int imax = 0;
for (int i = 1; i <= n; i++){
int poz = cauta(v[i].poz - 1);
prec[v[i].poz] = poz;
d[v[i].poz] = d[poz] + 1;
if (d[v[i].poz] > d[imax])
imax = v[i].poz;
update(v[i].poz);
}
fout << d[imax] << '\n';
while (imax > 0){
st.push(imax);
imax = prec[imax];
}
while (!st.empty()){
int i = st.top();
st.pop();
fout << init[i] << ' ';
}
fout << '\n';
}
int main(){
read();
solve();
return 0;
}