Submission #657434

#TimeUsernameProblemLanguageResultExecution timeMemory
657434bicsiOlympiads (BOI19_olympiads)C++14
100 / 100
1105 ms112760 KiB
#include <bits/stdc++.h> using namespace std; int main() { int n, k, c; cin >> n >> k >> c; vector<vector<int>> M(n, vector<int>(k)); for (int i = 0; i < n; ++i) for (int j = 0; j < k; ++j) cin >> M[i][j]; auto push = [&](vector<int>& dp, vector<int>& v, vector<int>& ret, vector<int>& trace) { fill(trace.begin(), trace.end(), -1); for (int sub = 0; sub < (1 << k); ++sub) { int extra = 0; for (int i = 0; i < k; ++i) if (sub & (1 << i)) extra += v[i]; for (int msk = sub; msk < (1 << k); msk = ((msk + 1) | sub)) { if (dp[msk ^ sub] < 0) continue; int now = dp[msk ^ sub] + extra; if (ret[msk] < now) { ret[msk] = now; trace[msk] = sub; } } } }; vector<vector<vector<int>>> suff(n + 1, vector<vector<int>>(k + 1, vector<int>(1 << k, -1))); auto trace = suff; suff[n][0][0] = 0; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j <= k; ++j) suff[i][j] = suff[i + 1][j]; for (int j = 0; j < k; ++j) push(suff[i + 1][j], M[i], suff[i][j + 1], trace[i][j + 1]); } priority_queue<tuple<int, int, int, vector<int>>> pq; auto push_state = [&](int i, int j, vector<int>& pref) { int score = -1; for (int msk = 0; msk < (1 << k); ++msk) { int oth = (((1 << k) - 1) ^ msk); if (pref[msk] < 0 || suff[i][k - j][oth] < 0) continue; score = max(score, pref[msk] + suff[i][k - j][oth]); } pq.emplace(score, i, j, pref); }; vector<int> init(1 << k, -1); init[0] = 0; push_state(0, 0, init); vector<int> buff(1 << k), npref(1 << k); for (int it = 0; it < c; ++it) { auto [score, pi, pj, pref] = pq.top(); pq.pop(); if (it == c - 1) { cout << score << endl; return 0; } int msk = 0; for (msk = 0; msk < (1 << k); ++msk) { int oth = (((1 << k) - 1) ^ msk); if (pref[oth] < 0 || suff[pi][k - pj][msk] < 0) continue; if (score == pref[oth] + suff[pi][k - pj][msk]) break; } int j = pj; for (int i = pi; i < n; ++i) { fill(npref.begin(), npref.end(), -1); push(pref, M[i], npref, buff); if (trace[i][k - j][msk] == -1) { // doesn't take if (j < k) push_state(i + 1, j + 1, npref); } else { // takes push_state(i + 1, j, pref); msk ^= trace[i][k - j][msk]; ++j; swap(pref, npref); } } } return 0; }

Compilation message (stderr)

olympiads.cpp: In function 'int main()':
olympiads.cpp:56:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |     auto [score, pi, pj, pref] = pq.top(); pq.pop();
      |          ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...