# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393546 | SirCovidThe19th | Longest beautiful sequence (IZhO17_subsequence) | C++14 | 22 ms | 45432 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;
/*
dp[i][j][k] - given that there must be k set bits in i(which is a mask
for possible values of first 10 bits of another value that is valid
with the current value)
and j is last 10 bits - j consists of only values we have seen
so far -> crucial to understanding
first loop through all possible values for j(read dp description).
we can then calculate(variable b)
the amount of set bits required in i(which is the left 10).
so: dp[left][possible value][k] -> we do this to find index
with longest chain that includes the current value
then we update dp array. loop through all possible values
for i; given that j(read dp description) is right(the variable).
*/
int main(){
int n;
cin >> n;
int a[n+1], k[n+1];
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> k[i];
int popCnt[1024];
for (int i = 0; i < 1024; i++) popCnt[i] = __builtin_popcount(i);
static int dp[1024][1024][11];
memset(dp, 0, sizeof(dp));
//at index i what is the longest chain you can form
int best[n+1];
fill(best, best+n+1, 0);
//at index i what is the first element in the longest
//chain you can form so far
int store[n+1];
for (int i = 1; i <= n; i++){
int right = a[i]&1023;
int left = a[i]>>10;
int idx = 0;
for (int mask = 0; mask < 1024; mask++){
int b = k[i]-popCnt[(right&mask)];
if (b >= 0 and b <= 10)
if (best[dp[left][mask][b]] > best[idx])
idx = dp[left][mask][b];
}
best[i] = best[idx]+1;
store[i] = idx;
for (int mask = 0; mask < 1024; mask++){
int b = max(popCnt[(left&mask)]-popCnt[right], 0);
if (best[i] > best[dp[mask][right][b]])
dp[mask][right][b] = i;
}
}
int pos = max_element(best, best+n+1)-best;
vector<int> res;
while (pos != 0){
res.push_back(pos);
pos = store[pos];
}
reverse(res.begin(), res.end());
cout<<res.size()<<endl;
for (int i = 0; i < res.size(); i++) cout<<res[i]<<" ";
}
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... |