Submission #1091725

#TimeUsernameProblemLanguageResultExecution timeMemory
1091725ULTRABIG7Longest beautiful sequence (IZhO17_subsequence)C++14
23 / 100
359 ms262144 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; using db = long double; #define ff first #define ss second #define pb push_back #define all(x) begin(x),end(x) #define FOR(i,a,b) for(int i = (a);i<(b);i++) #define F0R(i,a) FOR(i,0,a) #define rep(a) F0R(_,a) #define each(a,x) for(auto& a:x) #define sz(x) (int)size(x) #define vi vector<int> const int MOD = 1e9+7; const db PI = acos((db)-1); int main(){ cin.tie(0)->sync_with_stdio(0); int n; cin>>n; vi A(n+1), K(n+1); int mx = -1; FOR(i,1,n+1){ cin>>A[i]; mx = max(mx,A[i]); } FOR(i,1,n+1){ cin>>K[i]; } if(n<=5000){ vi dp(n+1,1), memo(n+1,-1); FOR(i,1,n+1){ FOR(j,1,i){ if(__builtin_popcount(A[i]&A[j]) == K[i] && dp[j]+1>dp[i]){ dp[i] = dp[j] + 1; memo[i] = j; } } } int ans = 0, id = -1; FOR(i,1,n+1){ if(dp[i]>ans){ id = i; ans = dp[i]; } } cout<<ans<<"\n"; vi sol; while(id != -1){ sol.pb(id); id = memo[id]; } reverse(all(sol)); each(x,sol){ cout<<x<<" "; } }else if(mx < 256){ vi dp(mx+1,-1),memo(mx+1,-1),ant(n+1,-1); FOR(i,1,n+1){ dp[A[i]] = 1; if(memo[A[i]] == -1) memo[A[i]] = i; } // dp(x) é a maior subsequência que termina no valor x FOR(i,1,n+1){ FOR(j,0,mx+1)if(dp[j] != -1 && __builtin_popcount(A[i] & j) == K[i]){ if(dp[j]+1 > dp[A[i]]){ dp[A[i]] = dp[j]+1; memo[A[i]] = i; ant[i] = memo[j]; } } } int ans = 0, val = -1; FOR(i,0,mx+1){ if(ans<dp[i]){ val = i; ans = dp[i]; } } cout<<ans<<"\n"; val = memo[val]; vi sol; while(val != -1){ sol.pb(val); val = ant[val]; } reverse(all(sol)); each(x,sol){ cout<<x<<" "; } } 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...