This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
5
5 3 5 3 5
10 1 20 1 20
4
1 2 3 4
10 0 1 0
4
1 2 3 4
10 0 1 0
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define N 100005
#define bit(x,y) ((x>>y)&1LL)
#define na(x) (#x) << ":" << x
ostream& operator << (ostream &os, vector<int>&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
cout << x.fi << sp << x.se << sp;
return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
return os;
}
int n;
int a[N], b[N];
int dp[N];
int par[N];
vector<int> order;
pair<int, int> bestMask[(1 << 8)];
void solveSub2() {
pair<int, int> ans = mp(1, 1);
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int j = 1; j <= i - 1; j++) {
if (dp[j] + 1 > dp[i] && __builtin_popcount(a[j]&a[i]) == b[i]) {
dp[i] = dp[j] + 1;
par[i] = j;
ans = max(ans, mp(dp[i], i));
}
}
}
cout << ans.fi << endl;
while (ans.fi >= 1) {
order.push_back(ans.se);
ans.se = par[ans.se];
ans.fi--;
}
reverse(order.begin(), order.end());
cout << order << endl;
}
set<int> st;
void solveSub3() {
pair<int, int> ans = mp(1, 1);
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int mask = 0; mask < (1 << 8); mask++) {
if (bestMask[mask].fi != 0)
assert(a[bestMask[mask].se] == mask);
if (bestMask[mask].fi + 1 > dp[i] && __builtin_popcount(mask & a[i]) == b[i]) {
dp[i] = bestMask[mask].fi + 1;
par[i] = bestMask[mask].se;
ans = max(ans, mp(dp[i], i));
}
}
if (bestMask[a[i]].fi < dp[i]) {
bestMask[a[i]] = mp(dp[i], i);
}
}
cout << ans.fi << endl;
while (ans.fi >= 1) {
order.push_back(ans.se);
ans.se = par[ans.se];
ans.fi--;
}
reverse(order.begin(), order.end());
for (int i = 1; i < order.size(); i++) {
if (__builtin_popcount(a[order[i - 1]]&a[order[i]]) != b[order[i]]) {
cout << order[i - 1] << sp << order[i] << endl;
}
}
cout << order << endl;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef in1code
freopen("1test.inp", "r", stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
if (n <= 5000) solveSub2();
else solveSub3();
}
Compilation message (stderr)
subsequence.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<int>&)':
subsequence.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
^
subsequence.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<int, int> >&)':
subsequence.cpp:31:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
^
subsequence.cpp: In function 'void solveSub3()':
subsequence.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < order.size(); i++) {
^
# | 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... |