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 <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, sub1 = 5e3 + 5, sub2 = 1e3 + 5;
int n, A[N], K[N], dp[sub1], dp2[sub2];
void Sub1()
{
int ans = 0;
for (int i = 1; i <= n; ++i)
{
dp[i] = 1;
for (int j = i - 1; j >= 1; --j)
if (__builtin_popcount(A[i] & A[j]) == K[i])
dp[i] = max(dp[i], dp[j] + 1);
ans = max(ans, dp[i]);
}
cout << ans;
}
void Sub2()
{
int ans = 0;
for (int i = 1; i <= n; ++i)
{
int maxDp = dp2[A[i]];
if (__builtin_popcount(A[i]) == K[i])
maxDp = dp2[A[i]] + 1;
for (int j = 0; j < sub2; ++j)
if (j != A[i] && __builtin_popcount(A[i] & j) == K[i])
maxDp = max(maxDp, dp2[j] + 1);
ans = max(ans, dp2[A[i]] = maxDp);
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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)
Sub1();
else
Sub2();
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... |