Submission #92716

#TimeUsernameProblemLanguageResultExecution timeMemory
92716janchomathLongest beautiful sequence (IZhO17_subsequence)C++14
0 / 100
2 ms380 KiB
#include<bits/stdc++.h> #define ll int #define f first #define s second #define pb push_back using namespace std; ll n,a[100005],k[100005],dp[5001][5001],ans,ind,p,anss[5001],maybe=1,mx,in[100005]; vector<ll>v; pair<ll,ll>x[300]; int main(){ std::ios::sync_with_stdio(false); cin >> n; for(int i=1; i<=n; i++){ cin >> a[i]; if(a[i] > 256){ maybe = 0; } } for(int i=1; i<=n; i++){ cin >> k[i]; } if(maybe){ for(int i=1; i<=n; i++)in[i] = i; for(int i=1; i<=n; i++){ mx = 0; ind = -1; for(int j=0; j<=256; j++){ if(__builtin_popcount((j&a[i])) == k[i]){ if(x[j].f > mx){ mx = x[j].f; ind = x[j].s; } } } //cout << ind << endl; if(!x[a[i]].f)x[a[i]] = {1LL,(ll)i}; if(ind == -1)continue; x[a[i]] = {mx+1LL,i}; in[i] = ind; } mx = 0; for(int i=1; i<=n; i++){ if(mx < x[a[i]].f){ mx = x[a[i]].f; p = i; } } //cout << p << endl; cout << mx << endl; while(in[p] != p){ v.pb(p); p = in[p]; } v.pb(p); for(int i=(int)v.size()-1; i>=0; i--){ cout << v[i] << " "; } return 0; } for(int i=1; i<=n; i++)dp[i][1] = i,anss[i]=1; ans = 1; ind = 1; for(int i=1; i<=n; i++){ p = -1; mx = 0; for(int j = i-1; j>=1; j--){ if(__builtin_popcount((a[j]&a[i])) == k[i]){ if(mx < anss[j]){ mx = anss[j]; p = j; } } } if(p==-1)continue; for(int j=2; j<=n; j++){ if(dp[p][j-1])dp[i][j] = p,anss[i] = j; if(dp[i][j] && j > ans){ ans = j; ind = i; } } } cout << ans << endl; while(dp[ind][ans] != ind){ v.pb(ind); ind = dp[ind][ans]; ans--; } v.pb(ind); for(int i=(int)v.size()-1; i>=0; i--){ cout << v[i] << " "; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...