Pagini recente » Cod sursa (job #776810) | Cod sursa (job #1748775) | Cod sursa (job #1070555) | Cod sursa (job #1735391) | Cod sursa (job #3239874)
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
std::string file = "scmax";
std::ifstream fin(file + ".in");
std::ofstream fout(file + ".out");
// #define fin std::cin
// #define fout std::cout
int n;
int lisLength = -1, lisEnd;
std::vector<int> v, dp, parent;
int32_t main(int32_t argc, char *argv[]) {
ios_base::sync_with_stdio(false);
fin.tie(0);
fout.tie(0);
fin >> n;
v.resize(n + 1);
dp.resize(n + 1);
parent.resize(n + 1, -1);
for (int i = 1; i <= n; ++i) {
fin >> v[i];
dp[i] = 1;
}
for (int i = 1; i <= n; ++i) {
for (int j = i - 1; j >= 1; --j) {
if (v[i] > v[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
parent[i] = j;
}
}
if (dp[i] > lisLength) {
lisLength = dp[i];
lisEnd = i;
}
}
fout << lisLength << "\n";
std::vector<int> lis;
while (lisEnd != -1) {
lis.push_back(lisEnd);
lisEnd = parent[lisEnd];
}
reverse(lis.begin(), lis.end());
for (const auto &el : lis) {
fout << v[el] << " ";
}
fout << "\n";
return 0;
}