Pagini recente » Cod sursa (job #1981571) | Cod sursa (job #581898) | Cod sursa (job #298908) | Cod sursa (job #822467) | Cod sursa (job #1469205)
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define mp make_pair
using namespace std;
const string name = "scmax",
in_file = name + ".in",
out_file = name + ".out";
ifstream fin(in_file);
ofstream fout(out_file);
const int MAX = 1e5 + 5;
int v[MAX], aib[MAX], n, sol, old[MAX], fmm[MAX], poz_max, father[MAX];
pii aux[MAX];
vector<int> mia;
int lsb(int x){
return x & (-x);
}
void update(int poz, int val, int current){
for (int i = poz; i <= n; i += lsb(i))
if(val > aib[i]){
aib[i] = val;
fmm[i] = current;
}
}
int query(int poz, int& poz_max){
int ret = 0;
for (int i = poz; i >= 1; i -= lsb(i))
if (aib[i] > ret){
ret = aib[i];
poz_max = i;
}
return ret;
}
int main(){
fin >> n;
for (int i = 1; i <= n; i++){
fin >> v[i];
pii elem = mp(v[i], i);
aux[i] = elem;
}
sort(aux + 1, aux + n + 1);
old[1] = v[aux[1].s];
v[aux[1].s] = 1;
for (int i = 2; i <= n; i++){
int temp = v[aux[i - 1].s];
if (aux[i].f != aux[i - 1].f){
temp++;
old[temp] = aux[i].f;
}
v[aux[i].s] = temp;
}
for (int poz, i = 1; i <= n; i++){
poz = 0;
int temp = 1 + query(v[i] - 1, poz);
//fout << "i = " << i << " temp = " << temp << " poz = " << poz << endl;
if (temp > sol){
sol = temp;
poz_max = i;
}
father[i] = fmm[poz];
update(v[i], temp, i);
}
fout << sol << '\n';
mia.pb(poz_max);
while (father[poz_max]){
mia.pb(father[poz_max]);
poz_max = father[poz_max];
}
while(mia.size()){
fout << old[v[mia.back()]] << ' ';
mia.pop_back();
}
return 0;
}