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<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<string>
#include<time.h>
#include<iomanip>
#include<assert.h>
#include<math.h>
#include<chrono>
#include<random>
#include<bitset>
using namespace std;
const int64_t INF = (int64_t) 5 * 1e5 + 5;
const int64_t mINF = (int64_t) 1e9 + 5;
const int64_t MOD = (int64_t) 1e9 + 9;
const int nbit = 30;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
struct Solution
{
int n;
vector<int> a;
vector<int> k;
Solution() {}
void solve()
{
cin >> n;
a.resize(n, 0);
k.resize(n, 0);
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
cin >> k[i];
}
if(n <= 1e3) Sub12();
else Sub3();
}
void Sub3()
{
int max_ = (1 << 8) + 5;
vector<int> dp(n, 1);
vector<int> p(n, -1);
vector<int> pos(max_, -1);
for(int i = 0; i < n; i++)
{
int id = -1;
for(int val = 0; val < max_; val++)
{
int cnt = __builtin_popcount(a[i] & val);
if(cnt != k[i] || pos[val] == -1) continue;
if(dp[i] < dp[pos[val]] + 1)
{
dp[i] = dp[pos[val]] + 1;
id = pos[val];
}
}
p[i] = id;
if(pos[a[i]] == -1 || (pos[a[i]] != -1 && dp[pos[a[i]]] < dp[i])) pos[a[i]] = i;
}
int e = -1;
int ans = 0;
for(int i = 0; i < n; i++)
{
if(ans >= dp[i]) continue;
e = i;
ans = max(ans, dp[i]);
}
vector<int> path;
while(e != -1)
{
path.push_back(e + 1);
e = p[e];
}
cout << ans << "\n";
reverse(path.begin(), path.end());
for(int i = 0; i < (int) path.size(); i++)
{
cout << path[i] << " ";
}
cout << "\n";
}
void Sub12()
{
vector<int> dp(n, 1);
vector<int> p(n, -1);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
{
int b = __builtin_popcount((a[j] & a[i]));
if(b != k[i] || dp[i] >= dp[j] + 1) continue;
p[i] = j;
dp[i] = dp[j] + 1;
}
}
int e = -1;
int ans = 0;
for(int i = 0; i < n; i++)
{
if(ans >= dp[i]) continue;
e = i;
ans = max(ans, dp[i]);
}
vector<int> path;
while(e != -1)
{
path.push_back(e + 1);
e = p[e];
}
cout << ans << "\n";
reverse(path.begin(), path.end());
for(int i = 0; i < (int) path.size(); i++)
{
cout << path[i] << " ";
}
cout << "\n";
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
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... |