# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
305108 | LucaDantas | Longest beautiful sequence (IZhO17_subsequence) | C++17 | 520 ms | 2944 KiB |
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;
using pii = pair<int, int>;
#define ff first
#define ss second
#define pb push_back
constexpr int maxn = 4e5+10;
int a[maxn], k[maxn], n;
int dp[maxn], dp_mask[1<<20]; // for the 3rd subtask
int ant[maxn], pos_mask[1<<20];
void print(int ans) {
printf("%d\n", dp[ans]);
vector<int> resp;
while(ans) {
resp.pb(ans);
ans = ant[ans];
}
reverse(resp.begin(), resp.end());
for(auto it : resp)
printf("%d ", it);
printf("\n");
}
void sub3() {
for(int i = 1; i <= n; i++) {
if(a[i] > (1<<9)) assert(0);
dp[i] = 1;
for(int mask = 0; mask < (1<<10); mask++)
if(__builtin_popcount(mask&a[i]) == k[i] && dp[i] <= dp_mask[mask])
dp[i] = dp_mask[mask]+1, ant[i] = pos_mask[mask];
if(dp[i] > dp_mask[a[i]])
dp_mask[a[i]] = dp[i], pos_mask[a[i]] = i;
}
}
void sub2() {
for(int i = 1; i <= n; i++) {
dp[i] = 1;
for(int j = 1; j < i; j++)
if(__builtin_popcount(a[j]&a[i]) == k[i] && dp[i] <= dp[j])
dp[i] = dp[j]+1, ant[i] = j;
}
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &k[i]);
if(n > 5000) sub3();
else sub2();
int ans = 0;
for(int i = 1; i <= n; i++)
if(dp[i] >= dp[ans])
ans = i;
print(ans);
}
Compilation message (stderr)
# | 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... |