This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, k;
long long sum, mx, a[N];
vector< vector< pair<long long, int> > > blocks;
vector< vector<long long> > res;
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i], sum += a[i], mx = max(mx, a[i]);
}
if (sum % k || mx > sum / k) {
cout << -1; return 0;
}
long long avg = sum / k;
long long cur = 0;
vector< pair<long long, int > > vec;
for (int i = 1; i <= n; ++i) {
cur += a[i];
if (cur >= avg) {
vec.push_back(make_pair(a[i] - (cur - avg), i));
blocks.push_back(vec), vec.clear(), cur -= avg;
if (cur) {
vec.push_back(make_pair(cur, i));
}
}
else vec.push_back(make_pair(a[i], i));
}
/* for (int i = 0; i < k; ++i) {
cout << "#" << i << '\n';
for (auto j : blocks[i]) cout << j.first << ' ' << j.second << '\n';
}*/
while (sum) {
long long mn = 1e18;
for (int i = 0; i < k; ++i) {
mn = min(mn, blocks[i].back().first);
}
sum -= mn * k;
vector<long long> vec;
vec.push_back(mn);
for (int i = 0; i < k; ++i) {
blocks[i][blocks[i].size() - 1].first -= mn;
vec.push_back(blocks[i].back().second);
if (!blocks[i].back().first) blocks[i].pop_back();
}
res.push_back(vec);
}
cout << res.size() << '\n';
for (int i = 0; i < res.size(); ++i) {
for (auto j : res[i]) cout << j << ' '; cout << '\n';
}
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < res.size(); ++i) {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |