이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC target ("arch=sandybridge")
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define IO ios_base::sync_with_stdio(0); cin.tie(0)
#define F first
#define S second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n;
int a[101010], k[101010], chain[101010];
int bits(int x) {
return __builtin_popcount(x);
}
int par[101010];
pii dp[101010];
int main() {
IO; cin>>n;
FOR(i,1,n+1) cin>>a[i];
FOR(i,1,n+1) cin>>k[i];
if (n > 5000) {
int start = 0;
FOR(i,1,n+1) {
chain[i] = 1;
// Normally we'd need to check all j < i for maximum dp.
// Since max value is 2^8-1, we can check all dp values in the range
// 0..255 due to the fact that the max AND result is 7 bits.
FOR(j,0,256) {
if ((bits(a[i] & j) == k[i]) && dp[j].F >= chain[i]) {
chain[i] = dp[j].F + 1;
par[i] = dp[j].S;
}
}
if (chain[i] >= dp[a[i]].F) {
dp[a[i]].F = chain[i];
dp[a[i]].S = i;
}
if (chain[i] > chain[start]) start = i;
}
vector<int> ans;
for (; start >= 1; ans.pb(start), start = par[start]);
sort(ans.begin(), ans.end());
cout<<ans.size()<<"\n";
for (int v : ans)cout<<v<<" ";
return 0;
}
int start = 0;
FOR(i,1,n+1) {
chain[i] = 1;
FOR(j,1,i) {
bool c = chain[j] >= chain[i];
if ((bits(a[i] & a[j]) == k[i]) && c) {
par[i] = j;
chain[i] = chain[j] + 1;
}
}
if (chain[i] > chain[start]) start = i;
}
vector<int> ans;
for (; start >= 1; ans.pb(start), start = par[start]);
sort(ans.begin(), ans.end());
cout<<ans.size()<<"\n";
for (int v : ans)cout<<v<<" ";
}
# | 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... |