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 <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
using namespace std;
const long long oo = 1000000000000000000;
long long int sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int dp[ 100001 ], p[ 100001 ], mp[ 100001 ];
vector <int> v;
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
int a[ n + 1 ];
int b[ n + 1 ];
for ( int i = 1; i <= n; i ++ ) {
cin >> a[ i ];
mx = max( mx, a[ i ] + 0ll );
dp[ i ] = 1;
p[ i ] = -1;
}
for ( int j = 1; j <= n; j ++ ) {
cin >> b[ j ];
}
if ( mx > 1000 ) {
for ( int i = 2; i <= n; i ++ ) {
for ( int j = 1; j < i; j ++ ) {
if ( __builtin_popcount( a[ i ] & a[ j ] ) == b[ i ] && dp[ i ] < dp[ j ] + 1 ) {
dp[ i ] = dp[ j ] + 1;
p[ i ] = j;
}
}
}
mx = 0;
for ( int i = 1; i <= n; i ++ ) {
if ( dp[ i ] > mx ) {
mx = dp[ i ];
pos = i;
}
}
}
else {
for ( int i = 1; i <= n; i ++ ) {
dp[ i ] = 1;
p[ i ] = -1;
}
mp[ a[ 1 ] ] = 1;
for ( int i = 2; i <= n; i ++ ) {
for ( int j = 0; j <= mx; j ++ ) {
if ( mp[ j ] && __builtin_popcount( a[ i ] & j ) == b[ i ] && dp[ i ] < dp[ mp[ j ] ] + 1 ) {
dp[ i ] = dp[ mp[ j ] ] + 1;
p[ i ] = mp[ j ];
}
}
if ( !mp[ a[ i ] ] || dp[ i ] > dp[ mp[ a[ i ] ] ] )
mp[ a[ i ] ] = i;
}
mx = 0;
for ( int i = 1; i <= n; i ++ ) {
if ( dp[ i ] > mx ) {
mx = dp[ i ];
pos = i;
}
}
}
cout << mx << "\n";
for ( int i = pos; i != -1; i = p[ i ] ) {
v.push_back( i );
}
for ( int i = v.size() - 1; i >= 0; i -- )
cout << v[ i ] << " ";
}
# | 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... |