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 <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cstdlib>
#include <bitset>
#include <deque>
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3fll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5005;
int a[maxn], k[maxn];
int dp[maxn], pre[maxn];
int n;
void sub12() {
for (int i = 1; i <= n; i++) dp[i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (__builtin_popcount(a[i] & a[j]) == k[i]) {
if (dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
pre[i] = j;
}
}
}
}
int mx = 0, pos = 0;
for (int i = 1; i <= n; i++) {
if (dp[i] > mx) {
mx = dp[i];
pos = i;
}
}
cout << mx << endl;
stack<int> s;
while (pos) {
s.push(pos);
pos = pre[pos];
}
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
}
int last[maxn][256];
void sub3() {
memset(last, -1, sizeof(last));
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 256; j++) last[i][j] = last[i - 1][j];
last[i][a[i]] = i;
}
for (int i = 0; i < 256; i++) dp[i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 256; j++) {
if (last[i][j] != -1 && __builtin_popcount(a[i] & j) == k[i]) {
if (a[i] == j) {
if (last[i - 1][a[i]] != -1) {
dp[a[i]]++;
pre[a[i]] = last[i - 1][a[i]];
}
continue;
}
if (dp[a[i]] < dp[j] + 1) {
dp[a[i]] = dp[j] + 1;
pre[a[i]] = j;
}
}
}
}
int mx = 0, pos = 0;
for (int i = 0; i < 256; i++) {
if (dp[i] > mx) {
mx = dp[i];
pos = i;
}
}
cout << mx << endl;
while (pos) {
cout << pos << " ";
pos = pre[a[pos]];
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> k[i];
}
if (n <= 5000) {
sub12();
} else {
sub3();
}
return 0;
}
# | 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... |