# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
746208 | PVSekhar | Longest beautiful sequence (IZhO17_subsequence) | C++14 | 113 ms | 262144 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;
#define ll long long
const ll MOD = 1e9+7;
const ll N=(1<<10);
int a[21],k[21];
int dp[N][N][11]; //prev right,cur left half,extra k to match cur left half
int ind[N][N][11]; //ind used to reach this state
int bc[N][N]; //bit count
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<21;k++)dp[i][j][k]=0;
for(int i=0;i<N;i++)for(int j=0;j<N;j++)for(int k=0;k<21;k++)ind[i][j][k]=-1;
for(int i=0;i<N;i++)for(int j=0;j<N;j++)bc[i][j]=__builtin_popcount(i&j);
int n;
cin>>n;
for (int i = 0; i < n; i++)cin>>a[i];
for (int i = 0; i < n; i++)cin>>k[i];
int p[n]; //stores previous index
for(int i=0;i<n;i++)p[i]=i;
int ans=0,ans_i,len,extra;
for (int i = 0; i < n; i++)
{
int l=a[i]>>(10),r=a[i]%(N);
// cout<<l<<" "<<r<<"\n";
len=1;
for (int j = 0; j < N; j++)
{
extra=k[i]-bc[j][r];
if(extra<0||extra>10)continue;
if(len<(dp[j][l][extra]+1)){
len=dp[j][l][extra]+1;
p[i]=ind[j][l][extra];
}
}
for (int j = 0; j < N; j++)
{
if(len>dp[r][j][bc[j][l]]){
dp[r][j][bc[j][l]]=len;
ind[r][j][bc[j][l]]=i;
// cout<<j<<"\n";
}
}
if(len>ans){
ans=len;
ans_i=i;
}
// cout<<ans<<"\n";
}
cout<<ans<<"\n";
vector<int> temp;
while(p[ans_i]!=ans_i){
temp.push_back(ans_i);
ans_i=p[ans_i];
}
temp.push_back(ans_i);
for (int i = ans-1; i >=0 ; i--)
{
cout<<temp[i]+1<<" ";
}
cout<<"\n";
return 0;
}
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... |