Submission #1098522

#TimeUsernameProblemLanguageResultExecution timeMemory
1098522TrinhKhanhDungLongest beautiful sequence (IZhO17_subsequence)C++14
100 / 100
2849 ms109712 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} const int MAX = 1e5 + 10; int N; int A[MAX], K[MAX]; void solve(){ cin >> N; for(int i = 1; i <= N; i++) cin >> A[i]; for(int i = 1; i <= N; i++) cin >> K[i]; } namespace subtask2{ int dp[MAX], trace[MAX]; void solve(){ int ans = 0, pos; 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(maximize(dp[i], dp[j] + 1)){ trace[i] = j; } } } if(maximize(ans, dp[i])) pos = i; } vector<int> sets; for(int i = 0; i < ans; i++){ sets.push_back(pos); pos = trace[pos]; } reverse(ALL(sets)); cout << ans << '\n'; for(int p: sets) cout << p << ' '; cout << '\n'; } } namespace subtask4{ const int M = 10; pair<int, int> dp[MASK(10) + 3][MASK(10) + 3][13]; int trace[MAX]; void solve(){ int p = 0, ans = 0; for(int i = 1; i <= N; i++){ int best = 1, pref = 0, suff = 0; pref = A[i] >> M; suff = A[i] & (MASK(M) - 1); for(int mask = 0; mask < MASK(M); mask++){ int need = K[i] - __builtin_popcount(mask & suff); if(need < 0 || need > M) continue; if(maximize(best, dp[pref][mask][need].fi + 1)){ trace[i] = dp[pref][mask][need].se; } } if(maximize(ans, best)) p = i; for(int mask = 0; mask < MASK(M); mask++){ if(maximize(dp[mask][suff][__builtin_popcount(mask & pref)].fi, best)){ dp[mask][suff][__builtin_popcount(mask & pref)].se = i; } } } cout << ans << '\n'; vector<int> sets; for(int i = 0; i < ans; i++){ sets.push_back(p); p = trace[p]; } reverse(ALL(sets)); for(int x: sets) cout << x << ' '; cout << '\n'; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("partner.inp","r",stdin); // freopen("partner.out","w",stdout); int t = 1; // cin >> t; while(t--){ solve(); subtask4::solve(); } 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...