This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author : Lăng Trọng Đạt
* created: 25-12-2023
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int int64_t
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
int g[MAXN], k[MAXN], dp[MAXN], p[MAXN];
pii dp1[1 << 9]; // dp1[i][x]: LBS ending in x
int n;
int last = 0;
void sub1() {
for (int i = 0; i < n; i++) {
dp[i] = 1;
p[i] = i;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (__builtin_popcount(g[i] & g[j]) == k[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
p[i] = j;
}
}
if (dp[i] > dp[last]) {
last = i;
}
}
cout << dp[last] << "\n";
}
void sub3() {
// memset(dp1, -1, sizeof(dp1));
for (int i = 0; i < n; i++) {
dp1[g[i]].f = 1;
dp1[g[i]].s = -1;
p[i] = i;
}
for (int i = 0; i< n; i++) {
if (dp1[g[i]].s == -1)
dp1[g[i]].s = i;
}
for (int i = 0; i < n; i++) {
bool id = i & 1;
for (int j = 0; j < (1 << 8); j++) {
if (__builtin_popcount(g[i] & j) == k[i] && dp1[j].f + 1 > dp1[g[i]].f && dp1[j].f != -1) {
// db(g[i], j, dp1[j], dp1[g[i]].f)
dp1[g[i]].f = dp1[j].f + 1;
dp1[g[i]].s = i;
p[i] = dp1[j].s;
}
}
if (dp1[g[i]].f > dp1[g[last]].f) {
db(i, g[i], dp1[g[i]].f)
last = i;
}
}
cout << dp1[g[last]].f << "\n";
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> g[i];
}
for (int i = 0; i < n; i++) {
std::cin >> k[i];
}
if (n <= 5000) {
sub1();
} else {
sub3();
}
db(last)
vector<int> ans = {last};
while (last != p[last]) {
db(last, dp1[last], p[last])
last = p[last];
ans.pb(last);
}
// cout << ans.size() << "\n";
db(ans)
for (int i = (int)ans.size() - 1; i >= 0; i--) {
cout << ans[i] + 1 << " ";
}
}
Compilation message (stderr)
subsequence.cpp: In function 'void sub3()':
subsequence.cpp:54:14: warning: unused variable 'id' [-Wunused-variable]
54 | bool id = i & 1;
| ^~
subsequence.cpp: In function 'int32_t main()':
subsequence.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("hi.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |