제출 #1006688

#제출 시각아이디문제언어결과실행 시간메모리
1006688Muaath_5Longest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
118 ms2140 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; const int N = 1e5+5; int n, a[N], k[N]; int dp[N]; int prv[N]; void sub1_2() { for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) { if (__builtin_popcount(a[i]&a[j]) == k[i]) { if (dp[j]+1 > dp[i]) { dp[i] = dp[j]+1; prv[i] = j; } } } } int idx = max_element(dp, dp+n+1)-dp; vector<int> lis; while (idx) { lis.push_back(idx); idx = prv[idx]; } reverse(lis.begin(), lis.end()); cout << lis.size() << '\n'; for (int i:lis) cout << i << ' '; exit(0); } int lst[1<<8]; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin >> n; for (int i = 1; i <= n; cin >> a[i++]); for (int i = 1; i <= n; cin >> k[i++]); if (n <= 5000) sub1_2(); for (int i = 1; i <= n; i++) { dp[i] = 1; for (int jj = 0; jj < (1<<8); jj++) { if (!lst[jj]) continue; const int j = lst[jj]; assert(jj == a[j]); if (__builtin_popcount(a[i]&a[j]) == k[i]) { if (dp[j]+1 >= dp[i]) { dp[i] = dp[j]+1; prv[i] = j; } } } if (dp[i] > dp[lst[a[i]]]) lst[a[i]] = i; } // for (int i = 1; i <= n; i++) cout << dp[i] << ' '; // cout << endl; int idx = max_element(dp, dp+n+1)-dp; vector<int> lis; while (idx) { lis.push_back(idx); idx = prv[idx]; } reverse(lis.begin(), lis.end()); cout << lis.size() << '\n'; for (int i:lis) cout << i << ' '; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...