#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LLA(x) x.rbegin(),x.rend()
#define print(x) for(auto el:x)cout<<el<<' ';cout<<"\n";
#define printPII(x) for(auto [a,b]:x)cout<<a<<","<<b<<' ';cout<<'\n';
using namespace std;
using ll = long long;
using PII = pair <ll, ll>;
ll n, k, onePick = 1;
bool impossible;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> k;
vector <vector <ll> > ans;
set <PII> bfs;
ll sum = 0;
vector <ll> vals, vals1;
for (int i = 1; i <= n; i++) {
ll x; cin >> x;
sum += x;
vals.pb(x);
bfs.insert({x, i});
}
sort(LLA(vals));
if (sum % k != 0) {
cout << "-1\n";
return 0;
}
if (vals[0] == vals.back()) {
ll cycleLen = k * n / gcd(k, n); // 4, 6, 2-> 12
ll oneCycleContribution = cycleLen / n; //
onePick = vals[0] / oneCycleContribution;
for (auto& el : vals) el /= onePick;
sum /= onePick;
}
while(!impossible && sum > 0) {
vector <ll> pick(1, onePick);
vector <PII> add;
for (int i = 0; i < k; i++) {
auto it = --bfs.end();
PII cur = *it;
bfs.erase(it);
pick.pb(cur.ss);
cur.ff--;
sum--;
if (cur.ff < 0) {
impossible = 1;
break;
}
add.pb(cur);
}
ans.pb(pick);
for (auto el : add) bfs.insert(el);
}
// int dbg = 0;
if (impossible) {
cout << "-1\n";
return 0;
}
cout << ans.size() << '\n';
for (auto el : ans) {
print(el);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
3 ms |
972 KB |
n=8 |
9 |
Correct |
8 ms |
1160 KB |
n=14 |
10 |
Correct |
5 ms |
972 KB |
n=11 |
11 |
Correct |
54 ms |
7320 KB |
n=50000 |
12 |
Correct |
52 ms |
7348 KB |
n=50000 |
13 |
Correct |
30 ms |
3320 KB |
n=10 |
14 |
Correct |
31 ms |
3112 KB |
n=685 |
15 |
Correct |
32 ms |
3276 KB |
n=623 |
16 |
Correct |
19 ms |
2032 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
3 ms |
972 KB |
n=8 |
9 |
Correct |
8 ms |
1160 KB |
n=14 |
10 |
Correct |
5 ms |
972 KB |
n=11 |
11 |
Correct |
54 ms |
7320 KB |
n=50000 |
12 |
Correct |
52 ms |
7348 KB |
n=50000 |
13 |
Correct |
30 ms |
3320 KB |
n=10 |
14 |
Correct |
31 ms |
3112 KB |
n=685 |
15 |
Correct |
32 ms |
3276 KB |
n=623 |
16 |
Correct |
19 ms |
2032 KB |
n=973 |
17 |
Correct |
42 ms |
2996 KB |
n=989 |
18 |
Correct |
13 ms |
1220 KB |
n=563 |
19 |
Correct |
25 ms |
1740 KB |
n=592 |
20 |
Correct |
20 ms |
1792 KB |
n=938 |
21 |
Correct |
17 ms |
1368 KB |
n=747 |
22 |
Correct |
19 ms |
1604 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
954 ms |
112640 KB |
n=1000000 |
2 |
Correct |
634 ms |
79964 KB |
n=666666 |
3 |
Correct |
330 ms |
45484 KB |
n=400000 |
4 |
Correct |
822 ms |
70764 KB |
n=285714 |
5 |
Correct |
16 ms |
2248 KB |
n=20000 |
6 |
Correct |
751 ms |
55176 KB |
n=181818 |
7 |
Correct |
11 ms |
1288 KB |
n=10000 |
8 |
Correct |
83 ms |
5108 KB |
n=6666 |
9 |
Correct |
4 ms |
652 KB |
n=4000 |
10 |
Correct |
525 ms |
25788 KB |
n=2857 |
11 |
Correct |
1 ms |
460 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
3 ms |
972 KB |
n=8 |
9 |
Correct |
8 ms |
1160 KB |
n=14 |
10 |
Correct |
5 ms |
972 KB |
n=11 |
11 |
Correct |
54 ms |
7320 KB |
n=50000 |
12 |
Correct |
52 ms |
7348 KB |
n=50000 |
13 |
Correct |
30 ms |
3320 KB |
n=10 |
14 |
Correct |
31 ms |
3112 KB |
n=685 |
15 |
Correct |
32 ms |
3276 KB |
n=623 |
16 |
Correct |
19 ms |
2032 KB |
n=973 |
17 |
Correct |
42 ms |
2996 KB |
n=989 |
18 |
Correct |
13 ms |
1220 KB |
n=563 |
19 |
Correct |
25 ms |
1740 KB |
n=592 |
20 |
Correct |
20 ms |
1792 KB |
n=938 |
21 |
Correct |
17 ms |
1368 KB |
n=747 |
22 |
Correct |
19 ms |
1604 KB |
n=991 |
23 |
Correct |
954 ms |
112640 KB |
n=1000000 |
24 |
Correct |
634 ms |
79964 KB |
n=666666 |
25 |
Correct |
330 ms |
45484 KB |
n=400000 |
26 |
Correct |
822 ms |
70764 KB |
n=285714 |
27 |
Correct |
16 ms |
2248 KB |
n=20000 |
28 |
Correct |
751 ms |
55176 KB |
n=181818 |
29 |
Correct |
11 ms |
1288 KB |
n=10000 |
30 |
Correct |
83 ms |
5108 KB |
n=6666 |
31 |
Correct |
4 ms |
652 KB |
n=4000 |
32 |
Correct |
525 ms |
25788 KB |
n=2857 |
33 |
Correct |
1 ms |
460 KB |
n=2000 |
34 |
Execution timed out |
2033 ms |
330332 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |