이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int a[n], k[n];
bool over511 = false;
for(int i = 0; i < n; i++) {
cin >> a[i];
if(a[i]>511) over511=true;
}
for(int i = 0; i < n; i++) cin >> k[i];
if(over511) {
vector<ll> longestEnd(n, 1); //longestEnd[a]=b: ends at index a, longest = b.
vector<int> prev(n, -1);
for(int i = 1; i < n; i++) {
for(int j = 0; j < i; j++) {
if(__builtin_popcountll(a[i]&a[j])==k[i]) {
if(longestEnd[j]+1>longestEnd[i]) {
longestEnd[i]=longestEnd[j]+1;
prev[i]=j;
}
}
}
}
int bestIndex = 0;
int most = longestEnd[0];
for(int i = 1; i < n; i++) {
if(longestEnd[i]>most) {
most=longestEnd[i];
bestIndex=i;
}
}
string ans = to_string(1+bestIndex);
while(prev[bestIndex]!=-1) {
bestIndex=prev[bestIndex];
ans=to_string(1+bestIndex)+" "+ans;
}
cout << most << endl << ans << endl;
} else {
vector<int> longest(512, -1);
vector<ll> longestEnd(n, 1);
vector<int> prev(n, -1);
longest[a[0]]=0;
for(int i = 1; i < n; i++) {
for(int j = 0; j < 512; j++) {
if(__builtin_popcountll(j&a[i])==k[i]) {
if(longest[j]==-1) continue;
if(longestEnd[longest[j]]+1>longestEnd[i]) {
longestEnd[i]=longestEnd[longest[j]]+1;
prev[i]=longest[j];
}
}
}
if(longest[a[i]]==-1||longestEnd[i]>longestEnd[longest[a[i]]]) {
longest[a[i]]=i;
}
}
int bestIndex = 0;
int most = longestEnd[0];
for(int i = 1; i < n; i++) {
if(longestEnd[i]>most) {
most=longestEnd[i];
bestIndex=i;
}
}
string ans = to_string(1+bestIndex);
while(prev[bestIndex]!=-1) {
bestIndex=prev[bestIndex];
ans=to_string(1+bestIndex)+" "+ans;
}
cout << most << endl << ans << endl;
}
}
/*
4
1 2 3 4
10 0 1 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... |