#include <bits/stdc++.h>
using namespace std;
#define int long long
#define x first
#define y second
const int INF = 1e18;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
set<pair<int, int>> m;
int total = 0;
set<int> ele;
for (int i = 0; i < n; i++) {
int p;
cin >> p;
total += p;
m.insert({p, i + 1});
ele.insert(p);
}
if (total % k != 0) {
cout << -1 << endl;
return 0;
}
if (n == k) {
if (ele.size() == 1) {
cout << 1 << endl;
cout << *ele.begin() << ' ';
for (int i = 0; i < k; i++) {
cout << i + 1 << ' ';
}
cout << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
vector<vector<int>> ans;
while (m.size() > k + 1) {
pair<int, int> d = *m.begin();
int mn = d.x;
m.erase(m.begin());
vector<pair<int, int>> v;
v.push_back(d);
for (int i = 0; i < k - 1; i++) {
v.push_back(*m.rbegin());
m.erase(--m.end());
mn = min(mn, v.back().first - 1);
}
mn = 1;
vector<int> idx(k);
for (int i = 0; i < k; i++) {
idx[i] = v[i].y;
v[i].x -= mn;
}
idx.push_back(mn);
reverse(idx.begin(), idx.end());
ans.push_back(idx);
for (int i = 0; i < k; i++) {
if (v[i].x > 0) {
m.insert(v[i]);
}
}
}
total = 0;
for (auto p : m) {
total += p.x;
}
int need = total / k;
vector<pair<int, int>> tmp(m.begin(), m.end());
int sz = tmp.size();
for (int i = 0; i < sz; i++) {
int d = need - tmp[i].x;
if (d < 0) {
cout << -1 << endl;
return 0;
}
if (d == 0) {
continue;
}
vector<int> idx;
for (int j = 0; j < sz; j++) {
if (i == j)
continue;
idx.push_back(tmp[j].second);
}
idx.push_back(d);
reverse(idx.begin(), idx.end());
ans.push_back(idx);
}
cout << ans.size() << endl;
for (auto &v : ans) {
for (auto &j : v) {
cout << j << ' ';
}
cout << endl;
}
return 0;
}
Compilation message
nicegift.cpp: In function 'int32_t main()':
nicegift.cpp:41:21: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
41 | while (m.size() > k + 1) {
| ~~~~~~~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
1 ms |
600 KB |
n=4 |
5 |
Correct |
1 ms |
344 KB |
n=4 |
6 |
Correct |
1 ms |
344 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
1 ms |
600 KB |
n=4 |
5 |
Correct |
1 ms |
344 KB |
n=4 |
6 |
Correct |
1 ms |
344 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
0 ms |
344 KB |
n=8 |
9 |
Correct |
8 ms |
856 KB |
n=14 |
10 |
Correct |
2 ms |
344 KB |
n=11 |
11 |
Correct |
88 ms |
5392 KB |
n=50000 |
12 |
Correct |
85 ms |
5380 KB |
n=50000 |
13 |
Correct |
21 ms |
1172 KB |
n=10 |
14 |
Correct |
64 ms |
2996 KB |
n=685 |
15 |
Correct |
78 ms |
3636 KB |
n=623 |
16 |
Correct |
38 ms |
2060 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
1 ms |
600 KB |
n=4 |
5 |
Correct |
1 ms |
344 KB |
n=4 |
6 |
Correct |
1 ms |
344 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
0 ms |
344 KB |
n=8 |
9 |
Correct |
8 ms |
856 KB |
n=14 |
10 |
Correct |
2 ms |
344 KB |
n=11 |
11 |
Correct |
88 ms |
5392 KB |
n=50000 |
12 |
Correct |
85 ms |
5380 KB |
n=50000 |
13 |
Correct |
21 ms |
1172 KB |
n=10 |
14 |
Correct |
64 ms |
2996 KB |
n=685 |
15 |
Correct |
78 ms |
3636 KB |
n=623 |
16 |
Correct |
38 ms |
2060 KB |
n=973 |
17 |
Incorrect |
54 ms |
3052 KB |
Not all heaps are empty in the end |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2054 ms |
266164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
1 ms |
600 KB |
n=4 |
5 |
Correct |
1 ms |
344 KB |
n=4 |
6 |
Correct |
1 ms |
344 KB |
n=2 |
7 |
Correct |
0 ms |
344 KB |
n=5 |
8 |
Correct |
0 ms |
344 KB |
n=8 |
9 |
Correct |
8 ms |
856 KB |
n=14 |
10 |
Correct |
2 ms |
344 KB |
n=11 |
11 |
Correct |
88 ms |
5392 KB |
n=50000 |
12 |
Correct |
85 ms |
5380 KB |
n=50000 |
13 |
Correct |
21 ms |
1172 KB |
n=10 |
14 |
Correct |
64 ms |
2996 KB |
n=685 |
15 |
Correct |
78 ms |
3636 KB |
n=623 |
16 |
Correct |
38 ms |
2060 KB |
n=973 |
17 |
Incorrect |
54 ms |
3052 KB |
Not all heaps are empty in the end |
18 |
Halted |
0 ms |
0 KB |
- |