This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LINE "--------------------\n"
#define ALL(x) x.begin(),x.end()
using namespace std;
using PII = pair <int, int>;
const int N = 1e5+5;
const int M = (1<<10);
int n;
int nums[N], bits[N], res[N], par[N];
PII dp[M][M][11]; // first value, second parent
int match[M][M];
int bitCnt(int a) {
return bitset <20>(a).count();
}
signed main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int ans = 1;
for (int i = 1; i <= n; i++) cin >> nums[i];
for (int i = 1; i <= n; i++) cin >> bits[i];
for (int i = 0; i < M; i++)
for (int j = i+1; j < M; j++) { // 2^20
match[i][j] = match[j][i] = bitCnt(i&j);
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) { // n*2^20
PII cur = {0, 0};
int lhs = nums[i]>>10;
int rhs = nums[i]%(1<<10);
// cout << LINE;
// cout << nums[i] << " " << lhs << ' ' << rhs << '\n';
for (int mask = 0; mask < M; mask++) { // 2^10
int matched = match[lhs][mask], tar = bits[i] - matched;
// cout << mask << ' ' << rhs << " : " << tar << " " << matched << '\n';
if (matched > bits[i] || (tar > 10)) continue;
// cout << "open\n";
if (dp[mask][rhs][tar].ff > cur.ff) cur = dp[mask][rhs][tar];
}
// cout << cur.ff << ' ' << cur.ss << '\n';
res[i] = cur.ff+1;
par[i] = cur.ss;
for (int mask = 0; mask < M; mask++) { // 2^20
// if ((mask&rhs) != rhs) continue;
int matched = match[rhs][mask];
if (dp[lhs][mask][matched].ff < res[i]) {
// cout << lhs << ' ' << mask << " " << matched << ' ' << res[i]+1 << "\n";
dp[lhs][mask][matched] = {res[i], i};
}
}
if (res[i] > res[ans]) ans = i;
// cout << dp[i] <<"," << par[i] << " ";
}
// cout << "\n";
vector <int> tmp;
while(ans != 0) {
tmp.pb(ans);
ans = par[ans];
}
cout << tmp.size() << '\n';
while(!tmp.empty()) {
cout << tmp.back() << " ";
tmp.pop_back();
}
cout << '\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... |