Submission #1066337

#TimeUsernameProblemLanguageResultExecution timeMemory
1066337MilosMilutinovicCookies (JOI23_cookies)C++14
12 / 100
400 ms1048576 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) array<int, 3> prv[N][N][N]; 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; prv[i][j][k] = {-1, 0, 0}; } } } 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 <= s; k++) { prv[i][j][k] = {i - 1, 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; prv[i][j + 1][k + b[i]] = {i, j, k}; } } } } 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 (prv[i][j][k][0] != i) { 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...