#include <bits/stdc++.h>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define all(v) v.begin(),v.end()
#define pb push_back
#define sz size()
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef unsigned long long ull;
const int N = 1e6 + 5;
const ll M = 1e8;
const ll inf = 1e18;
const ll mod = 1e9;
const double Pi = acos(-1);
ll binpow(ll x, ll ti) { ll res = 1;while (ti){if(ti & 1)res *= x;x *= x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll binmul(ll x, ll ti) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= mod; res %= mod;} return res;}
ll nok(ll a, ll b) { return (a*b)/__gcd(abs(a),abs(b)) * (a*b > 0 ? 1 : -1); }
bool odd(ll n) { return (n % 2 == 1); }
bool even(ll n) { return (n % 2 == 0); }
ll n, k, sum, l[N], mx, cnt;
pll a[N], b[N];
vector <pll> q[N];
const void solve(/*Armashka*/) {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) cin >> a[i].ft, a[i].sd = i;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++ i) b[i] = a[i], sum += a[i].ft, mx = max(mx, a[i].ft);
if (sum % k || mx > sum / k) {
cout << "-1\n";
return;
}
ll s = 0, cur = 1;
for (int i = 1; i <= n; ++ i) {
if (s + a[i].ft <= sum / k) {
q[cur].pb({a[i].ft, a[i].sd});
s += a[i].ft;
if (s == sum / k) {
s = 0;
++ cur;
}
} else {
q[cur].pb({(sum / k) - s, a[i].sd});
++ cur;
s = a[i].ft - ((sum / k) - s);
q[cur].pb({s, a[i].sd});
}
}
while (1) {
multiset <pll> st;
for (int i = 1; i <= k; ++ i) {
if (l[i] < q[i].sz) st.insert({q[i][l[i]].ft, i});
}
if (st.empty()) break;
for (auto [x, pos] : st) {
if (x == st.begin()->ft) q[pos][l[pos]].ft = 0, ++ l[pos];
else q[pos][l[pos]].ft -= st.begin()->ft;
}
++ cnt;
}
cout << cnt << "\n";
for (int i = 1; i <= k; ++ i) q[i].clear();
s = 0, cur = 1;
for (int i = 1; i <= n; ++ i) {
if (s + a[i].ft <= sum / k) {
q[cur].pb({a[i].ft, a[i].sd});
s += a[i].ft;
if (s == sum / k) {
s = 0;
++ cur;
}
} else {
q[cur].pb({(sum / k) - s, a[i].sd});
++ cur;
s = a[i].ft - ((sum / k) - s);
q[cur].pb({s, a[i].sd});
}
}
for (int i = 1; i <= k; ++ i) l[i] = 0;
while (1) {
multiset <pair<pll, ll>> st;
for (int i = 1; i <= k; ++ i) {
if (l[i] < q[i].sz) st.insert({q[i][l[i]], i});
}
if (st.empty()) break;
cout << st.begin()->ft.ft << " ";
for (auto [x, pos] : st) {
if (x.ft == st.begin()->ft.ft) q[pos][l[pos]].ft = 0, ++ l[pos];
else q[pos][l[pos]].ft -= st.begin()->ft.ft;
cout << x.sd << " ";
}
cout << "\n";
}
}
signed main() {
fast;
//freopen("divide.in", "r", stdin);
//freopen("divide.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt --) {
solve();
}
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
Compilation message
nicegift.cpp: In function 'const void solve()':
nicegift.cpp:63:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | if (l[i] < q[i].sz) st.insert({q[i][l[i]].ft, i});
| ^
nicegift.cpp:94:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | if (l[i] < q[i].sz) st.insert({q[i][l[i]], i});
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
11 ms |
23808 KB |
n=3 |
3 |
Correct |
11 ms |
23800 KB |
n=3 |
4 |
Correct |
11 ms |
23808 KB |
n=4 |
5 |
Correct |
11 ms |
23720 KB |
n=4 |
6 |
Correct |
11 ms |
23756 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
11 ms |
23808 KB |
n=3 |
3 |
Correct |
11 ms |
23800 KB |
n=3 |
4 |
Correct |
11 ms |
23808 KB |
n=4 |
5 |
Correct |
11 ms |
23720 KB |
n=4 |
6 |
Correct |
11 ms |
23756 KB |
n=2 |
7 |
Correct |
11 ms |
23756 KB |
n=5 |
8 |
Correct |
11 ms |
23708 KB |
n=8 |
9 |
Correct |
11 ms |
23708 KB |
n=14 |
10 |
Correct |
14 ms |
23884 KB |
n=11 |
11 |
Correct |
37 ms |
27144 KB |
n=50000 |
12 |
Correct |
38 ms |
27120 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
12 ms |
23884 KB |
n=685 |
15 |
Correct |
12 ms |
23804 KB |
n=623 |
16 |
Correct |
12 ms |
23884 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
11 ms |
23808 KB |
n=3 |
3 |
Correct |
11 ms |
23800 KB |
n=3 |
4 |
Correct |
11 ms |
23808 KB |
n=4 |
5 |
Correct |
11 ms |
23720 KB |
n=4 |
6 |
Correct |
11 ms |
23756 KB |
n=2 |
7 |
Correct |
11 ms |
23756 KB |
n=5 |
8 |
Correct |
11 ms |
23708 KB |
n=8 |
9 |
Correct |
11 ms |
23708 KB |
n=14 |
10 |
Correct |
14 ms |
23884 KB |
n=11 |
11 |
Correct |
37 ms |
27144 KB |
n=50000 |
12 |
Correct |
38 ms |
27120 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
12 ms |
23884 KB |
n=685 |
15 |
Correct |
12 ms |
23804 KB |
n=623 |
16 |
Correct |
12 ms |
23884 KB |
n=973 |
17 |
Correct |
12 ms |
23780 KB |
n=989 |
18 |
Correct |
13 ms |
23788 KB |
n=563 |
19 |
Correct |
16 ms |
23876 KB |
n=592 |
20 |
Correct |
15 ms |
23796 KB |
n=938 |
21 |
Correct |
15 ms |
23796 KB |
n=747 |
22 |
Correct |
14 ms |
23928 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
98760 KB |
n=1000000 |
2 |
Correct |
266 ms |
72236 KB |
n=666666 |
3 |
Correct |
157 ms |
52116 KB |
n=400000 |
4 |
Correct |
470 ms |
58588 KB |
n=285714 |
5 |
Correct |
21 ms |
25156 KB |
n=20000 |
6 |
Correct |
403 ms |
50400 KB |
n=181818 |
7 |
Correct |
16 ms |
24444 KB |
n=10000 |
8 |
Correct |
90 ms |
25832 KB |
n=6666 |
9 |
Correct |
13 ms |
24048 KB |
n=4000 |
10 |
Correct |
498 ms |
33356 KB |
n=2857 |
11 |
Correct |
13 ms |
23940 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
11 ms |
23808 KB |
n=3 |
3 |
Correct |
11 ms |
23800 KB |
n=3 |
4 |
Correct |
11 ms |
23808 KB |
n=4 |
5 |
Correct |
11 ms |
23720 KB |
n=4 |
6 |
Correct |
11 ms |
23756 KB |
n=2 |
7 |
Correct |
11 ms |
23756 KB |
n=5 |
8 |
Correct |
11 ms |
23708 KB |
n=8 |
9 |
Correct |
11 ms |
23708 KB |
n=14 |
10 |
Correct |
14 ms |
23884 KB |
n=11 |
11 |
Correct |
37 ms |
27144 KB |
n=50000 |
12 |
Correct |
38 ms |
27120 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
12 ms |
23884 KB |
n=685 |
15 |
Correct |
12 ms |
23804 KB |
n=623 |
16 |
Correct |
12 ms |
23884 KB |
n=973 |
17 |
Correct |
12 ms |
23780 KB |
n=989 |
18 |
Correct |
13 ms |
23788 KB |
n=563 |
19 |
Correct |
16 ms |
23876 KB |
n=592 |
20 |
Correct |
15 ms |
23796 KB |
n=938 |
21 |
Correct |
15 ms |
23796 KB |
n=747 |
22 |
Correct |
14 ms |
23928 KB |
n=991 |
23 |
Correct |
389 ms |
98760 KB |
n=1000000 |
24 |
Correct |
266 ms |
72236 KB |
n=666666 |
25 |
Correct |
157 ms |
52116 KB |
n=400000 |
26 |
Correct |
470 ms |
58588 KB |
n=285714 |
27 |
Correct |
21 ms |
25156 KB |
n=20000 |
28 |
Correct |
403 ms |
50400 KB |
n=181818 |
29 |
Correct |
16 ms |
24444 KB |
n=10000 |
30 |
Correct |
90 ms |
25832 KB |
n=6666 |
31 |
Correct |
13 ms |
24048 KB |
n=4000 |
32 |
Correct |
498 ms |
33356 KB |
n=2857 |
33 |
Correct |
13 ms |
23940 KB |
n=2000 |
34 |
Correct |
24 ms |
25588 KB |
n=23514 |
35 |
Correct |
25 ms |
25588 KB |
n=23514 |
36 |
Correct |
12 ms |
23884 KB |
n=940 |
37 |
Correct |
12 ms |
23756 KB |
n=2 |
38 |
Correct |
118 ms |
32228 KB |
n=100000 |
39 |
Correct |
119 ms |
32344 KB |
n=100000 |
40 |
Correct |
12 ms |
23796 KB |
n=10 |
41 |
Correct |
12 ms |
23800 KB |
n=100 |
42 |
Correct |
22 ms |
24072 KB |
n=1000 |
43 |
Correct |
504 ms |
107412 KB |
n=1000000 |
44 |
Correct |
629 ms |
111580 KB |
n=1000000 |
45 |
Correct |
550 ms |
87316 KB |
n=666666 |
46 |
Correct |
469 ms |
66676 KB |
n=400000 |
47 |
Correct |
536 ms |
33108 KB |
n=2336 |
48 |
Correct |
415 ms |
58596 KB |
n=285714 |
49 |
Correct |
420 ms |
50312 KB |
n=181818 |
50 |
Correct |
404 ms |
38276 KB |
n=40000 |
51 |
Correct |
441 ms |
36156 KB |
n=20000 |
52 |
Correct |
414 ms |
34120 KB |
n=10000 |
53 |
Correct |
459 ms |
33988 KB |
n=6666 |
54 |
Correct |
419 ms |
32580 KB |
n=4000 |
55 |
Correct |
496 ms |
33348 KB |
n=2857 |
56 |
Correct |
267 ms |
28420 KB |
n=2000 |