# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
395273 | rama_pang | Gift (IZhO18_nicegift) | C++17 | 683 ms | 124628 KiB |
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;
using lint = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, K;
cin >> N >> K;
lint total = 0;
vector<lint> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
total += A[i];
}
if (total % K != 0 || total / K < *max_element(begin(A), end(A))) {
cout << -1 << "\n";
return 0;
}
lint limit = total / K;
vector<lint> sum(K);
vector<vector<pair<lint, int>>> data(K);
for (int i = 0; i < N; i++) {
for (int j = 0; j < K && A[i] > 0; j++) {
if (sum[j] + A[i] <= limit) {
sum[j] += A[i];
data[j].emplace_back(A[i], i);
A[i] = 0;
} else if (sum[j] < limit) {
A[i] -= limit - sum[j];
data[j].emplace_back(limit - sum[j], i);
sum[j] = limit;
}
}
assert(A[i] == 0);
}
vector<pair<lint, vector<int>>> ans;
while (!data[0].empty()) {
lint minim = 1e18;
for (int i = 0; i < K; i++) {
minim = min(minim, data[i].back().first);
}
ans.emplace_back();
ans.back().first = minim;
for (int i = 0; i < K; i++) {
ans.back().second.emplace_back(data[i].back().second);
data[i].back().first -= minim;
if (data[i].back().first == 0) {
data[i].pop_back();
}
}
}
assert(ans.size() <= N);
cout << ans.size() << "\n";
for (auto i : ans) {
cout << i.first;
for (auto j : i.second) {
cout << " " << j + 1;
}
cout << "\n";
}
return 0;
}
Compilation message (stderr)
# | 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... |