이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int B = 8;
struct State {
int len, end;
};
int main() {
// preprocess:
// bc[i][j] = bit_count(i & j)
vector<vector<int>> bc(1 << B, vector<int>(1 << B));
for (int i = 0; i < (1 << B); i++) {
for (int j = 0; j < (1 << B); j++) {
bc[i][j] = __builtin_popcount(i & j);
}
}
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> k(n);
for (int i = 0; i < n; i++) cin >> k[i];
int ans = 1;
// best_i: most optimal index to end on
int best_i = 0;
vector<int> prv(n); // prv[i]: best index to include before i
iota(prv.begin(), prv.end(), 0); // prv[i] = i
vector<vector<State>> dp2(1 << B, vector<State>(B, State{0, -1}));
for (int i = 0; i < n; i++) {
// length of longest LBS that ends at i
int len = dp2[a[i]][k[i]].len + 1;
if (dp2[a[i]][k[i]].end != -1) { prv[i] = dp2[a[i]][k[i]].end; }
for (int j = 0; j < (1 << B); j++) {
State &affected_state = dp2[j][bc[a[i]][j]];
if (len > affected_state.len) { affected_state = State{len, i}; }
}
if (len > ans) {
ans = len;
best_i = i;
}
}
cout << ans << endl;
vector<int> res;
while (prv[best_i] != best_i) {
res.push_back(best_i);
best_i = prv[best_i];
}
res.push_back(best_i);
reverse(res.begin(), res.end());
for (int x : res) { cout << x + 1 << " "; }
}
# | 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... |