#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e6 + 7;
ll a[N];
signed main()
{
IO_OP;
int n, k;
cin >> n >> k;
ll sum = 0, mx = 0;
for(int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
mx = max(mx, a[i]);
}
if(sum % k || mx > sum / k) {
cout << -1 << '\n';
return 0;
}
V<array<ll, 3>> ev;
auto add_event = [&] (ll l, ll r, int i) { // [l, r)
assert(l < r);
ev.PB({l, 1, i});
ev.PB({r, -1, i});
};
ll psum = 0;
for(int i = 0; i < n; i++) {
if(a[i] >= sum / k) add_event(0, sum / k, i + 1); // [l, r)
else {
ll lb = psum % (sum / k), rb = (lb + a[i] - 1) % (sum / k);
if(lb <= rb) add_event(lb, rb + 1, i + 1);
else add_event(0, rb + 1, i), add_event(lb, sum / k, i);
}
psum += a[i];
}
sort(ALL(ev));
set<int> who;
int lst = -1;
V<ll> ans;
for(auto e:ev) {
if(lst != -1 && e[0] != lst) {
assert(int(who.size()) == k);
ll x = e[0] - lst;
ans.PB(x);
for(int i:who)
ans.PB(i);
}
if(e[1] == 1) { // add
assert(who.count(e[2]) == 0);
who.insert(e[2]);
} else {
assert(who.count(e[2]) == 1);
who.erase(e[2]);
}
assert(int(who.size()) <= k);
lst = e[0];
}
cout << int(ans.size()) / (k + 1) << '\n';
for(int i = 0; i < int(ans.size()); i++)
cout << ans[i] << " \n"[i % (k + 1) == k];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Incorrect |
1 ms |
364 KB |
Taken too much stones from the heap |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Incorrect |
1 ms |
364 KB |
Taken too much stones from the heap |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Incorrect |
1 ms |
364 KB |
Taken too much stones from the heap |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
785 ms |
111728 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Incorrect |
1 ms |
364 KB |
Taken too much stones from the heap |
5 |
Halted |
0 ms |
0 KB |
- |