Submission #1167170

#TimeUsernameProblemLanguageResultExecution timeMemory
1167170SmuggingSpunLongest beautiful sequence (IZhO17_subsequence)C++20
100 / 100
2530 ms178160 KiB
#include<bits/stdc++.h> #define taskname "C" using namespace std; template<class T>bool maximize(T& a, T b){ if(a < b){ a = b; return true; } return false; } const int lim = 1e5 + 5; int n, a[lim], k[lim]; namespace sub1{ void solve(){ int ans_mask = 0; for(int mask = (1 << n) - 1; mask > 0; mask--){ if(__builtin_popcount(mask) > __builtin_popcount(ans_mask)){ vector<int>p; for(int i = 0; i < n; i++){ if(1 << i & mask){ p.emplace_back(i + 1); } } bool flag = true; for(int i = 1; i < p.size(); i++){ if(__builtin_popcount(a[p[i]] & a[p[i - 1]]) != k[p[i]]){ flag = false; break; } } if(flag){ ans_mask = mask; } } } cout << __builtin_popcount(ans_mask) << "\n"; for(int i = 0; i < n; i++){ if(1 << i & ans_mask){ cout << i + 1 << " "; } } } } namespace sub2{ void solve(){ vector<int>dp(n + 1, 1), trace(n + 1, dp[0] = 0); for(int i = 1; i <= n; i++){ for(int j = 1; j < i; j++){ if(__builtin_popcount(a[i] & a[j]) == k[i] && maximize(dp[i], dp[j] + 1)){ trace[i] = j; } } } int I = 0; for(int i = 1; i <= n; i++){ if(dp[i] > dp[I]){ I = i; } } cout << dp[I] << "\n"; vector<int>ans; while(I != 0){ ans.emplace_back(I); I = trace[I]; } for(int i = ans.size(); i > 0; ){ cout << ans[--i] << " "; } } } namespace sub3{ void solve(){ vector<int>dp(n + 1, 1), trace(n + 1, dp[0] = 0), max_dp_pos(256, 0); for(int i = 1; i <= n; i++){ for(int j = 0; j < 256; j++){ if(max_dp_pos[j] != 0 && __builtin_popcount(j & a[i]) == k[i] && maximize(dp[i], dp[max_dp_pos[j]] + 1)){ trace[i] = max_dp_pos[j]; } } if(dp[i] > dp[max_dp_pos[a[i]]]){ max_dp_pos[a[i]] = i; } } int I = 0; for(int i = 1; i <= n; i++){ if(dp[i] > dp[I]){ I = i; } } cout << dp[I] << "\n"; vector<int>ans; while(I != 0){ ans.emplace_back(I); I = trace[I]; } for(int i = ans.size(); i > 0; ){ cout << ans[--i] << " "; } } } namespace sub4{ const int SIZE = 1 << 10; pair<int, int>dp[SIZE][SIZE][21]; int trace[lim], bit_count[SIZE][SIZE]; void solve(){ for(int i = 0; i < SIZE; i++){ for(int j = i; j < SIZE; j++){ bit_count[i][j] = bit_count[j][i] = __builtin_popcount(i & j); fill(dp[i][j], dp[i][j] + 21, make_pair(-1, -1)); } } iota(trace + 1, trace + n + 1, 1); int best_i, best_ans = 0; for(int i = 1; i <= n; i++){ int L = a[i] & (SIZE - 1), R = a[i] >> 10, ans = 1, ans_end = i; for(int j = 0; j < SIZE; j++){ int need_k = k[i] - bit_count[L][j]; if(need_k > -1 && maximize(ans, dp[j][R][need_k].first + 1)){ trace[i] = dp[j][R][need_k].second; } } if(maximize(best_ans, ans)){ best_i = i; } for(int j = 0; j < SIZE; j++){ auto& [dp_ans, dp_end] = dp[L][j][bit_count[R][j]]; if(maximize(dp_ans, ans)){ dp_end = i; } } } vector<int>ans; while(best_i != trace[best_i]){ ans.emplace_back(best_i); best_i = trace[best_i]; } cout << best_ans << "\n" << best_i << " "; for(int i = ans.size(); i > 0; ){ cout << ans[--i] << " "; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen(taskname".inp", "r")){ freopen(taskname".inp", "r", stdin); } cin >> n; for(int i = 1; i <= n; i++){ cin >> a[i]; } for(int i = 1; i <= n; i++){ cin >> k[i]; } if(n <= 15){ sub1::solve(); } else if(n <= 5000){ sub2::solve(); } else if(*max_element(a + 1, a + n + 1) < 256){ sub3::solve(); } else{ sub4::solve(); } }

Compilation message (stderr)

subsequence.cpp: In function 'int main()':
subsequence.cpp:146:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...