이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 1e6 + 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];
}
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;
for( auto y : cnt ){
d[y].se = 0;
}
}
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();
}
}
컴파일 시 표준 에러 (stderr) 메시지
subsequence.cpp: In function 'void solve()':
subsequence.cpp:73:9: warning: unused variable 'mx' [-Wunused-variable]
int mx = 0;
^~
subsequence.cpp:74:9: warning: unused variable 'p' [-Wunused-variable]
int p = 0;
^
# | 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... |