이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair <int, int> pii;
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define F first
#define S second
const int N = 1e5 + 5;
const int MX = (1 << 10) + 5;
int n, a[N], k[N], pre[N], bit[MX];
pii dp[MX];
vector <int> vec;
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
cin >> k[i];
}
for (int i = 0; i < MX-5; ++i) {
bit[i] = __builtin_popcount(i);
}
for (int i = 1; i <= n; ++i) {
dp[a[i]] = max(dp[a[i]], {1, i});
if (bit[a[i]] < k[i]) { continue; }
for (int m = 0; m < MX-5; ++m) {
if (bit[m] < k[i]) { continue; }
if (bit[(a[i] & m)] != k[i]) { continue; }
if (dp[a[i]].F < dp[m].F + 1) {
dp[a[i]] = {dp[m].F + 1, i};
pre[i] = dp[m].S;
}
}
}
int ans = 0, pos = 0;
for (int i = 0; i < MX-5; ++i) {
if (ans < dp[i].F) {
ans = dp[i].F;
pos = dp[i].S;
}
}
while (pre[pos]) {
vec.pb(pos);
pos = pre[pos];
}
vec.pb(pos);
reverse(all(vec));
cout << sz(vec) <<"\n";
for (int i = 0; i < sz(vec); ++i) {
cout << vec[i] <<" ";
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
//cin >> tests;
while (tests --) {
solve();
}
return 0;
}
/*
Just Chalish!
4
1 2 3 4
10 0 1 0
2
8 9
20 0
5
5 3 5 3 5
10 1 20 1 20
*/
# | 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... |