Pagini recente » Cod sursa (job #1399489) | Cod sursa (job #2298773) | Cod sursa (job #1659996) | Cod sursa (job #3220462) | Cod sursa (job #2744299)
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
#define LSB(a) ((-a) & a)
using namespace std;
#ifdef LOCAL
#define read() ifstream fin("date.in.txt");
#else
#define read() ifstream fin("scmax.in"); ofstream fout("scmax.out");
#endif // LOCAL
const int N = 100005;
int n, AIB[N];
int v[N];
int actPosInVector[N];
map<int,int> idx;
set<int> vals;
vector<int> path[N];
int searchMax(int pos, int val) {
int pMax = 0;
while(pos > 0) {
if(AIB[pMax] < AIB[pos] && v[actPosInVector[pos]] < val)
pMax = pos;
pos -= LSB(pos);
}
return pMax;
}
void update(int pos, int val, int actAdded) {
while(pos <= n) {
if(AIB[pos] < val) {
AIB[pos] = val;
actPosInVector[pos] = actAdded;
}
else if(AIB[pos] == val && v[actPosInVector[pos]] > v[actAdded]) {
actPosInVector[pos] = actAdded;
}
pos += LSB(pos);
}
}
int main() {
read();
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
vals.insert(v[i]);
}
int p = 1;
for (int x : vals) {
idx[x] = p++;
}
for (int i = 1; i <= n; ++i) {
int posSecvMax = searchMax(idx[v[i]], v[i]);
//vector<int> secv = path[posSecvMax];
//secv.push_back(v[i]);
//cout << posSecvMax << " " << v[i] << '\n';
update(idx[v[i]], AIB[posSecvMax] + 1, i);
/*for (int j = 1; j <= n; ++j) {
cout << AIB[j] << " ";
}
cout << '\n';
for (int j = 1; j <= n; ++j) {
cout << v[actPosInVector[j]] << " ";
}
cout << '\n';
*/
}
int best = searchMax(n, INT_MAX);
#ifdef LOCAL
cout << AIB[best] << '\n';
int act = 1;
for (int i = 1; i <= best; ++i) {
if(AIB[i] == act) {
cout << v[actPosInVector[i]] << " ";
++act;
}
}
#else
fout << AIB[best] << '\n';
int act = 1;
for (int i = 1; i <= best; ++i) {
if(AIB[i] == act) {
fout << v[actPosInVector[i]] << " ";
++act;
}
}
#endif // LOCAL
return 0;
}