Submission #1066340

#TimeUsernameProblemLanguageResultExecution timeMemory
1066340MilosMilutinovicCookies (JOI23_cookies)C++14
63 / 100
297 ms496468 KiB
#include <bits/stdc++.h> using namespace std; const int N = 505; const int inf = (int) 1e9; int dp[N][N][N]; // (current box size, number of taken boxes, total sum of boxes) 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]; } reverse(b.begin(), b.end()); int s = accumulate(a.begin(), a.end(), 0); vector<long long> limit(s + 1); for (int i = 0; i < n; i++) { for (int j = 1; j <= s; j++) { limit[j] += min(j, a[i]); } } for (int i = 0; i < m; i++) { for (int j = 0; j <= s; j++) { for (int k = 0; k <= s; k++) { dp[i][j][k] = inf; } } } dp[0][0][0] = 0; for (int i = 0; i < m; i++) { if (i > 0) { for (int j = 0; j <= s; j++) { for (int k = 0; k <= limit[j]; k++) { dp[i][j][k] = dp[i - 1][j][k]; } } } for (int j = 0; j + 1 <= s; j++) { for (int k = 0; k + b[i] <= limit[j + 1]; k++) { if (dp[i][j + 1][k + b[i]] > dp[i][j][k] + 1) { dp[i][j + 1][k + b[i]] = dp[i][j][k] + 1; } } } } int p = 0; for (int i = 0; i <= s; i++) { if (dp[m - 1][i][s] < dp[m - 1][p][s]) { p = i; } } int ans = dp[m - 1][p][s]; if (ans == inf) { cout << -1 << '\n'; return 0; } cout << ans << '\n'; vector<int> v; int i = m - 1, j = p, k = s; while (i != -1) { if ((i == 0 && j == 0) || (i > 0 && dp[i][j][k] == dp[i - 1][j][k])) { i -= 1; continue; } v.push_back(b[i]); j -= 1; k -= b[i]; } sort(v.rbegin(), v.rend()); int sz = (int) v.size(); vector<vector<int>> ids(sz); set<pair<int, int>> st; for (int i = 0; i < n; i++) { st.emplace(a[i], i); } for (int i = 0; i < sz; i++) { vector<int> p; for (int foo = 0; foo < v[i]; foo++) { auto it = prev(st.end()); int id = it->second; st.erase(it); ids[i].emplace_back(id); a[id] -= 1; if (a[id] > 0) { p.push_back(id); } } for (int j : p) { st.emplace(a[j], j); } } for (int i = 0; i < sz; i++) { cout << (int) ids[i].size() << " "; for (int j : ids[i]) { cout << 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...