Submission #1005904

#TimeUsernameProblemLanguageResultExecution timeMemory
1005904MilosMilutinovicCookies (JOI23_cookies)C++14
6 / 100
4 ms6492 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int m; cin >> m; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b[i]; } const int MAX = 505; vector<int> cnt(MAX); for (int i = 0; i < n; i++) { cnt[a[i]] += 1; } for (int i = MAX - 1; i > 0; i--) { cnt[i - 1] += cnt[i]; } int mx = *max_element(a.begin(), a.end()); vector<vector<int>> dp(MAX, vector<int>(MAX, MAX)); vector<vector<pair<int, int>>> prv(MAX, vector<pair<int, int>>(MAX, {-1, -1})); dp[1][0] = 0; auto Update = [&](int i, int j, int x, int y, int f) { if (dp[i][j] > dp[x][y] + f) { dp[i][j] = dp[x][y] + f; prv[i][j] = {x, y}; } }; for (int c = 1; c <= mx; c++) { for (int x = 0; x <= cnt[c]; x++) { if (dp[c][x] == MAX) { continue; } for (int i = 0; i < m; i++) { if (b[i] > min(x, cnt[c + 1]) + (cnt[c] - x)) { continue; } int y = x + b[i]; if (y <= cnt[c]) { Update(c, y, c, x, 1); } else { Update(c + 1, y - cnt[c], c, x, 1); } } } Update(c + 1, 0, c, cnt[c], 0); } if (dp[mx + 1][0] == MAX) { cout << -1 << '\n'; return 0; } int x = mx + 1, y = 0; vector<int> bv; while (x > 0) { int pv; if (prv[x][y].first == -1) { pv = 0; } else if (prv[x][y].first == x) { pv = prv[x][y].second; } else { pv = prv[x][y].second - cnt[x - 1]; } if (y != pv) { bv.push_back(y - pv); } pair<int, int> p = prv[x][y]; x = p.first; y = p.second; } vector<int> order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int i, int j) { return a[i] > a[j]; }); vector<int> ids; for (int c = MAX; c >= 1; c--) { for (int i : order) { if (a[i] >= c) { ids.push_back(i); } } } reverse(ids.begin(), ids.end()); vector<vector<int>> res; for (int v : bv) { res.push_back({}); for (int i = 0; i < v; i++) { res.back().push_back(ids.back()); ids.pop_back(); } } cout << (int) res.size() << '\n'; for (int i = 0; i < (int) res.size(); i++) { cout << (int) res[i].size() << " "; for (int j = 0; j < (int) res[i].size(); j++) { cout << res[i][j] + 1 << " "; } cout << '\n'; } 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...