Submission #171152

#TimeUsernameProblemLanguageResultExecution timeMemory
171152muhammad_hokimiyonLongest beautiful sequence (IZhO17_subsequence)C++14
23 / 100
1079 ms2764 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define fi first #define se second #define ll long long using namespace std; const int N = 1e5 + 7; const int mod = 1e9 + 7; int n; int a[N]; int b[N]; int c[N]; pair < int , int > d[N]; pair < int , int > f[N]; void solve() { cin >> n; for( int i = 1; i <= n; i++ ){ cin >> a[i]; d[i] = {0 , 0}; //f[a[i]] = {0 , i}; } for( int i = 1; i <= n; i++ ){ cin >> b[i]; } if( n <= 5000 ){ int mx = 1; for( int i = 1; i <= n; i++ ){ d[i].se = -1; d[i].fi = 1; for( int j = 1; j < i; j++ ){ if( d[j].fi + 1 > d[i].fi && __builtin_popcount(a[j] & a[i]) == b[i] ){ d[i].fi = d[j].fi + 1; mx = max( mx , d[i].fi ); d[i].se = j; } } } int p = n; for( int i = 1; i <= n; i++ ){ if( d[i].fi == mx )p = i; } vector < int > ans; while( p != -1 ){ ans.push_back(p); p = d[p].se; } reverse( ans.begin() , ans.end() ); cout << mx << "\n"; for( auto x : ans ){ cout << x << " "; } return; } for( int i = 1; i <= n; i++ ){ for( int j = 0; j < (1 << 8); j++ ){ if( __builtin_popcount((a[i] & j)) == b[i] ){ if( d[i].fi < f[j].fi + 1 ){ d[i] = f[j]; d[i].fi += 1; } } } if( f[a[i]].fi < d[i].fi ){ f[a[i]] = d[i]; f[a[i]].se = i; } } int mx = 0; int p = 0; vector < int > ans; for( int i = n; i >= 1; i-- ){ vector < int > cnt; int x = i; while( x != 0 ){ cnt.push_back(x); x = d[x].se; } if( (int)cnt.size() > (int)ans.size() )ans = cnt; } reverse( ans.begin() , ans.end() ); cout << (int)ans.size() << "\n"; for( auto x : ans ){ cout << x << " "; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); int cghf = 1;//cin >> cghf; while( cghf-- ){ solve(); } }

Compilation message (stderr)

subsequence.cpp: In function 'void solve()':
subsequence.cpp:75:9: warning: unused variable 'mx' [-Wunused-variable]
     int mx = 0;
         ^~
subsequence.cpp:76:9: warning: unused variable 'p' [-Wunused-variable]
     int p = 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...