Submission #1070136

#TimeUsernameProblemLanguageResultExecution timeMemory
1070136VMaksimoski008Longest beautiful sequence (IZhO17_subsequence)C++17
0 / 100
264 ms262144 KiB
#include <bits/stdc++.h> #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() //#define int long long using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int mod = 1e9 + 7; const int LOG = 20; const int maxn = 1e5 + 5; const double eps = 1e-9; int32_t main() { int n; cin >> n; vector<int> a(n+1), k(n+1); for(int i=1; i<=n; i++) cin >> a[i]; for(int i=1; i<=n; i++) cin >> k[i]; if(*max_element(all(a)) < (1 << 8)) { pii dp[1<<8]; for(int i=0; i<(1<<8); i++) dp[i] = { 0, 0 }; vector<int> par(n+1, -1); set<int> s; for(int i=1; i<=n; i++) { dp[a[i]].first = max(dp[a[i]].first, 1); if(dp[a[i]].first == 1) dp[a[i]].second = i; for(auto &x : s) { if(__builtin_popcount(x & a[i]) == k[i]) { if(dp[a[i]].first < dp[x].first + 1) { dp[a[i]].first = dp[x].first + 1; dp[a[i]].second = i; par[i] = dp[x].second; } } } s.insert(a[i]); } int pos = dp[a[1]].second; for(int i=2; i<=n; i++) if(dp[a[i]].first > dp[a[pos]].first) pos = dp[a[i]].second; cout << dp[a[pos]].first << '\n'; vector<int> ans; while(pos != -1) { ans.push_back(pos); pos = par[pos]; } reverse(all(ans)); for(int &x : ans) cout << x << " "; return 0; } vector<int> dp(n+1), par(n+1, -1); for(int i=1; i<=n; i++) dp[i] = 1; for(int i=2; i<=n; i++) { for(int j=1; j<i; j++) { if(__builtin_popcount(a[i] & a[j]) == k[i] && dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; par[i] = j; } } } int pos = 0; for(int i=1; i<=n; i++) if(dp[i] > dp[pos]) pos = i; cout << dp[pos] << '\n'; vector<int> ans; while(pos != -1) { ans.push_back(pos); pos = par[pos]; } reverse(all(ans)); for(int &x : ans) 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...