Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #912875) | Cod sursa (job #1831957) | Cod sursa (job #3317768)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int main() {
ios::sync_with_stdio(false);
fin.tie(0);
int n;
fin >> n;
vector<long long> a(n);
for (int i = 0; i < n; ++i) fin >> a[i];
vector<long long> dp; // valorile finale minime
vector<int> pos; // pozițiile din a[]
vector<int> pred(n, -1); // precursorii pentru reconstrucție
for (int i = 0; i < n; ++i) {
// găsim locul unde putem pune a[i]
int j = lower_bound(dp.begin(), dp.end(), a[i]) - dp.begin();
if (j == (int)dp.size()) {
dp.push_back(a[i]);
pos.push_back(i);
} else {
dp[j] = a[i];
pos[j] = i;
}
if (j > 0) pred[i] = pos[j - 1];
}
int Lmax = dp.size();
fout << Lmax << "\n";
// reconstrucția
vector<long long> lis;
int p = pos[Lmax - 1];
while (p != -1) {
lis.push_back(a[p]);
p = pred[p];
}
reverse(lis.begin(), lis.end());
for (auto x : lis) fout << x << " ";
fout << "\n";
return 0;
}