이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <bit>
#include <map>
using std::cout;
using std::endl;
using std::vector;
using std::pair;
/**
* https://oj.uz/problem/view/IZhO17_subsequence
* 5
* 5 3 5 3 5
* 10 1 20 1 20 should output
* 2
* 1 2
*/
int main() {
int size;
std::cin >> size;
vector<int> arr(size);
for (int& i : arr) {
std::cin >> i;
}
vector<int> targets(size);
for (int& t : targets) {
std::cin >> t;
}
std::map<int, pair<int, int>> got_seqs{{arr[0], {1, 0}}};
vector<int> prev(size);
int longest = 1;
int longest_end = 0;
for (int i = 1; i < size; i++) {
int n = arr[i];
int this_max = 1;
prev[i] = i;
for (const auto& [p, p_info] : got_seqs) {
if (std::__popcount(n & p) == targets[i]) {
if (p_info.first + 1 > this_max) {
this_max = p_info.first + 1;
prev[i] = p_info.second;
}
}
}
if (this_max > got_seqs[n].first) {
got_seqs[n] = {this_max, i};
}
if (this_max > longest) {
longest = this_max;
longest_end = i;
}
}
vector<int> seq{longest_end};
while (prev[seq.back()] != seq.back()) {
seq.push_back(prev[seq.back()]);
}
cout << seq.size() << endl;
for (int i = seq.size() - 1; i > 0; i--) {
cout << seq[i] + 1 << ' ';
}
cout << seq.front() + 1 << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |