Submission #893013

#TimeUsernameProblemLanguageResultExecution timeMemory
893013NoLoveLongest beautiful sequence (IZhO17_subsequence)C++14
23 / 100
6056 ms4444 KiB
/** * author : Lăng Trọng Đạt * created: 25-12-2023 **/ #include <bits/stdc++.h> using namespace std; #ifndef LANG_DAT #define db(...) ; #endif // LANG_DAT #define int int64_t #define mp make_pair #define f first #define s second #define pb push_back #define all(v) (v).begin(), (v).end() using pii = pair<int, int>; const int MAXN = 1e5 + 5; const int BB = 8; int g[MAXN], k[MAXN], dp[MAXN], p[MAXN]; int bc[1 << BB][1 << BB]; // pii dp1[1 << 9]; // dp1[i][x]: LBS ending in x int n, res; int last = 0; void sub1() { for (int i = 0; i < n; i++) { dp[i] = 1; p[i] = i; } for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (__builtin_popcount(g[i] & g[j]) == k[i] && dp[j] + 1 > dp[i]) { // db(i, j, __builtin_popcount(g[i] & g[j]), k[i]) dp[i] = dp[j] + 1; p[i] = j; } } if (dp[i] > dp[last]) { last = i; } } res = dp[last]; } void sub3() { // memset(dp1, -1, sizeof(dp1)); vector<pii> dp1(1 << BB, {-1, -1}); for (int i = 0; i < n; i++) { // dp1[g[i]].f = 1; // dp1[g[i]].s = -1; p[i] = i; } // for (int i = 0; i< n; i++) { // if (dp1[g[i]].s == -1) // dp1[g[i]].s = i; // } for (int i = 0; i < n; i++) { bool id = i & 1; if (dp1[g[i]].f == -1) { dp1[g[i]] = {1, i}; } for (int j = 0; j < (1 << 8); j++) { if (bc[g[i]][j] == k[i] && dp1[j].f + 1 > dp1[g[i]].f && dp1[j].f != -1) { // db(g[i], j, dp1[j], dp1[g[i]].f) dp1[g[i]].f = dp1[j].f + 1; p[i] = dp1[j].s; dp1[g[i]].s = i; } } if (dp1[g[i]].f > dp1[g[last]].f) { // db(i, g[i], dp1[g[i]].f) last = i; } } db(dp1[g[4993]], p[4993], g[4993], k[4993]) res = dp1[g[last]].f; } const int B = 8; struct State { int len, end; }; void sub3_ans() { // preprocess: // bc[i][j] = bit_count(i & j) vector<vector<int>> bc(1 << B, vector<int>(1 << B)); for (int i = 0; i < (1 << B); i++) { for (int j = 0; j < (1 << B); j++) { bc[i][j] = __builtin_popcount(i & j); } } int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> k(n); for (int i = 0; i < n; i++) cin >> k[i]; int ans = 1; // best_i: most optimal index to end on int best_i = 0; vector<int> prv(n); // prv[i]: best index to include before i iota(prv.begin(), prv.end(), 0); // prv[i] = i vector<State> dp1(1 << B, State{-1, -1}); for (int i = 0; i < n; i++) { if (dp1[a[i]].len == -1) { dp1[a[i]].len = 1; dp1[a[i]].end = i; } for (int j = 0; j < (1 << B); j++) { if (bc[j][a[i]] == k[i] && dp1[j].len + 1 > dp1[a[i]].len) { dp1[a[i]].len = dp1[j].len + 1; dp1[a[i]].end = i; prv[i] = dp1[j].end; } } if (dp1[a[i]].len > ans) { ans = dp1[a[i]].len; best_i = i; } } cout << ans << endl; vector<int> res; while (prv[best_i] != best_i) { res.push_back(best_i); best_i = prv[best_i]; } res.push_back(best_i); reverse(res.begin(), res.end()); for (int x : res) { cout << x + 1 << " "; } } int32_t main() { cin.tie(0)->sync_with_stdio(0); if (fopen("hi.inp", "r")) { freopen("hi.inp", "r", stdin); freopen("hi2.out", "w", stdout); } // preprocess if (n <= 5000) { for (int i = 1; i < (1 << B); i++) for (int j = 1; j < (1 << B); j++) bc[i][j] = __builtin_popcount(i & j); std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> g[i]; } for (int i = 0; i < n; i++) { std::cin >> k[i]; } sub1(); vector<int> ans = {last}; while (last != p[last]) { // db(last, p[last]) last = p[last]; ans.pb(last); } cout << ans.size() << "\n"; // cout << ans.size() << "\n"; reverse(all(ans)); for (int i : ans) { cout << i + 1 << " "; } } else { sub3_ans(); } // db(last) // for (int i = (int)ans.size() - 1; i >= 0; i--) { // } // for (int i = 1; i < ans.size(); i++) { // if (__builtin_popcount(g[ans[i]] & g[ans[i - 1]]) != k[ans[i]]) { // db(ans[i], g[ans[i]], g[ans[i] - 1], __builtin_popcount(g[ans[i]] & g[ans[i - 1]]), k[i]) // } // } // db(ans.size(), res) }

Compilation message (stderr)

subsequence.cpp: In function 'void sub3()':
subsequence.cpp:58:14: warning: unused variable 'id' [-Wunused-variable]
   58 |         bool id = i & 1;
      |              ^~
subsequence.cpp: In function 'int32_t main()':
subsequence.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
subsequence.cpp:139:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |        freopen("hi2.out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...