# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
89505 | 2018-12-15T07:00:27 Z | Hideo | Gift (IZhO18_nicegift) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e6 + 7; const int INF = 1e9 + 7; int a[N], p[N]; int n, k, s; vector < pair < int, int > > dq[N]; vector < int > ans[N]; main(){ cin >> n >> k; int mx = -1; for (int i = 0; i < n; i++){ scanf("%lld", &a[i]); s += a[i]; mx = max(mx, a[i]); } int h = s / k, cs = 0, c = 0; if (mx > h || s % k != 0){ cout << -1; return 0; } for (int i = 0; i < n; i++){ if(a[i] == 0) continue; if (cs + a[i] >= h){ a[i] -= (h - cs); dq[c].push_back(make_pair(h - cs, i)); cs = 0; i--; c++; continue; } cs += a[i]; dq[c].push_back(make_pair(a[i], i)); } int g = 0; while (s != 0){ int mn = -1; for (int i = 0; i < k; i++){ if (mn == -1){ mn = dq[i][p[i]].first; continue; } mn = min(mn, dq[i][p[i]].first); } ans[g].push_back(mn); for (int i = 0; i < k; i++){ dq[i][p[i]].first -= mn; ans[g].push_back(dq[i][p[i]].second + 1); if(dq[i][p[i]].first == 0) p[i]++; } g++; s -= (mn * k); } cout << g << endl; int it = 0; while (it != g){ for (int i = 0; i < ans[it].size(); i++){ printf("%lld ", ans[it][i]) } printf("\n"); it++; } }