Pagini recente » Borderou de evaluare (job #17811) | Cod sursa (job #1997212)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
ifstream f("scmax.in");
streambuf* bufIn = f.rdbuf();
cin.rdbuf(bufIn);
ofstream g("scmax.out");
streambuf* bufOut = g.rdbuf();
cout.rdbuf(bufOut);
vector<int> vec;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
vec.push_back(x);
}
vector<int> lis, sol;
int maxLen = 0, len = 0;
for (auto &val: vec) {
auto pos = lower_bound(lis.begin(), lis.begin() + len, val);
if (pos == lis.end()) {
lis.push_back(val);
pos = lis.end() - 1;
}
else {
*pos = val;
}
int intPos = pos - lis.begin();
len = intPos + 1;
if (len > maxLen) {
maxLen = len;
sol = lis;
}
}
cout << maxLen << "\n";
for (auto &val : sol) {
cout << val << " ";
}
return 0;
}