이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX = (1 << 20);
const int TMP = (1 << 10) - 1;
const int N = 100005;
// 10 last bits: mask & TMP
// 10 first bits: mask >> 10
int n, a[N], k[N];
vector<int> G[MAX >> 10][11]; // G[mask][overlap]
int save[MAX][11]; // (dp, mask)
int trace[N], f[N];
void mxm(pair<int,int> &x, pair<int,int> y) {
if (x < y) x = y;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> k[i];
for (int mask = 0; mask < (MAX >> 10); ++mask) {
for (int nmask = 0; nmask < (MAX >> 10); ++nmask) {
int overlap = __builtin_popcount(mask & nmask);
G[mask][overlap].push_back(nmask);
}
}
// solve
for (int i = 1; i <= n; ++i) {
int dp = 0;
int suf = (a[i] & TMP);
int pre = (a[i] >> 10);
for (int k2 = 0; k2 <= min(10, k[i]); ++k2) {
int k1 = k[i] - k2;
if (k1 > 10) continue;
// find dp[i]
for (int nsuf : G[suf][k2]) {
if (dp < f[save[(pre << 10) + nsuf][k1]]) {
dp = f[save[(pre << 10) + nsuf][k1]];
trace[i] = save[(pre << 10) + nsuf][k1];
}
}
}
f[i] = ++dp;
// update dp[i] to other dps
for (int overlap = 0; overlap <= 10; ++overlap) {
for (int mask : G[pre][overlap]) {
if (f[i] > f[save[(mask << 10) + suf][overlap]]) {
save[(mask << 10) + suf][overlap] = i;
}
}
}
}
int mx = 0, pos = 0;
for (int i = 1; i <= n; ++i) if (f[i] > mx) mx = f[i], pos = i;
printf("%d\n", mx);
vector<int> ans;
while(pos > 0) {
ans.push_back(pos);
pos = trace[pos];
}
reverse(ans.begin(), ans.end());
for (int i : ans) printf("%d ", i); printf("\n");
}
# | 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... |