Pagini recente » Cod sursa (job #3163626) | Cod sursa (job #435999) | Cod sursa (job #1976214) | Cod sursa (job #640882) | Cod sursa (job #3129422)
#include <bits/stdc++.h>
void printVect(const std::vector<int>& v) {
for (auto x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
}
std::vector<int> bestSeq(const std::vector<int>& nums) {
// fill best
std::vector<int> best(nums.size());
best[0] = 1;
for (auto i = 1u; i < nums.size(); i++) {
int currentBest = 0;
for (int j = i - 1; j >= 0; --j) {
if (nums[i] > nums[j] && best[j] > currentBest) {
currentBest = best[j];
}
}
best[i] = currentBest + 1;
}
// find best length
int bestLength = *std::max_element(best.begin(), best.end());
// find the position where best length is
int bestPos = nums.size() - 1;
while (bestPos > 0) {
if (best[bestPos] == bestLength)
break;
bestPos--;
}
// fill the result
std::vector<int> res(bestLength);
for (int i = bestPos, idx = bestLength - 1; i >= 0; --i) {
if (best[i] == bestLength) {
res[idx--] = nums[i];
bestLength--;
}
}
return res;
}
int main() {
std::ifstream iStream("scmax.in");
int size{ 0 };
iStream >> size;
std::vector<int> nums(size);
for (int i = 0; i < size; i++)
iStream >> nums[i];
iStream.close();
std::ofstream oStream("scmax.out");
auto res = bestSeq(nums);
oStream << res.size() << '\n';
for (auto val : res)
oStream << val << ' ';
oStream.close();
return 0;
}