#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 1e6+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int a[N], n, m, k;
vector<pii> v[N];
int s[N];
void solve(int test_case){
int i, j;
cin >> n >> k;
int sum = 0;
int mx = 0;
for(i=0;i<n;i++){
cin >> a[i];
sum += a[i];mx = max(mx, a[i]);
}
if(sum%k != 0 || sum < mx*k){
cout << -1 << '\n';
return;
}
int c = 0;
int need = sum/k;
for(i=0;i<n;i++){
if(need==s[c]){
c++;
}
if(need-s[c] >= a[i]){
s[c] += a[i];
// cout << c << ';' << i << ' ' << a[i] << '\n';
v[c].pb({a[i], i+1});
}else {
v[c].pb({need-s[c], i+1});
a[i] -= need-s[c];
s[c] = need;i--;
}
}
/* for(i=0;i<k;i++){
cout << "i = " << i << '\n';
for(auto [x,y] : v[i]){
cout << x << ' ' << y << '\n';
}cout << "\n\n";
}*/
vector<vector<int>> ans;
int itr = 0;
while(true){
itr++;
// if(itr>100)break;
bool ok = true;
for(i=0;i<k;i++)if(v[i].size())ok = false;
if(ok)break;
int mn = 2e18;
for(i=0;i<k;i++){
if(!v[i].size())continue;
mn = min(v[i].back().ff, mn);
}
vector<int> tmp;
tmp.pb(mn);
for(i=0;i<k;i++){
if(!v[i].size())continue;
int lst = v[i].size()-1;
v[i][lst].ff -= mn;
tmp.pb(v[i][lst].ss);
if(v[i][lst].ff == 0)v[i].pop_back();
}
ans.pb(tmp);
}
cout << ans.size() << '\n';
for(auto X : ans){
for(auto x : X)cout << x << ' ';
cout << '\n';
}
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int ___T;
cin >> ___T;
for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
Compilation message
nicegift.cpp: In function 'void solve(long long int)':
nicegift.cpp:18:9: warning: unused variable 'j' [-Wunused-variable]
18 | int i, j;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23788 KB |
n=4 |
2 |
Correct |
17 ms |
23788 KB |
n=3 |
3 |
Correct |
17 ms |
23788 KB |
n=3 |
4 |
Correct |
18 ms |
23788 KB |
n=4 |
5 |
Correct |
18 ms |
23788 KB |
n=4 |
6 |
Correct |
17 ms |
23788 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23788 KB |
n=4 |
2 |
Correct |
17 ms |
23788 KB |
n=3 |
3 |
Correct |
17 ms |
23788 KB |
n=3 |
4 |
Correct |
18 ms |
23788 KB |
n=4 |
5 |
Correct |
18 ms |
23788 KB |
n=4 |
6 |
Correct |
17 ms |
23788 KB |
n=2 |
7 |
Correct |
17 ms |
23788 KB |
n=5 |
8 |
Correct |
17 ms |
23856 KB |
n=8 |
9 |
Correct |
16 ms |
23788 KB |
n=14 |
10 |
Correct |
18 ms |
23788 KB |
n=11 |
11 |
Correct |
44 ms |
28004 KB |
n=50000 |
12 |
Correct |
42 ms |
27880 KB |
n=50000 |
13 |
Correct |
19 ms |
23936 KB |
n=10 |
14 |
Correct |
20 ms |
23916 KB |
n=685 |
15 |
Correct |
20 ms |
23916 KB |
n=623 |
16 |
Correct |
20 ms |
23916 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23788 KB |
n=4 |
2 |
Correct |
17 ms |
23788 KB |
n=3 |
3 |
Correct |
17 ms |
23788 KB |
n=3 |
4 |
Correct |
18 ms |
23788 KB |
n=4 |
5 |
Correct |
18 ms |
23788 KB |
n=4 |
6 |
Correct |
17 ms |
23788 KB |
n=2 |
7 |
Correct |
17 ms |
23788 KB |
n=5 |
8 |
Correct |
17 ms |
23856 KB |
n=8 |
9 |
Correct |
16 ms |
23788 KB |
n=14 |
10 |
Correct |
18 ms |
23788 KB |
n=11 |
11 |
Correct |
44 ms |
28004 KB |
n=50000 |
12 |
Correct |
42 ms |
27880 KB |
n=50000 |
13 |
Correct |
19 ms |
23936 KB |
n=10 |
14 |
Correct |
20 ms |
23916 KB |
n=685 |
15 |
Correct |
20 ms |
23916 KB |
n=623 |
16 |
Correct |
20 ms |
23916 KB |
n=973 |
17 |
Correct |
22 ms |
23916 KB |
n=989 |
18 |
Correct |
18 ms |
23916 KB |
n=563 |
19 |
Correct |
22 ms |
24044 KB |
n=592 |
20 |
Correct |
19 ms |
24044 KB |
n=938 |
21 |
Correct |
17 ms |
24044 KB |
n=747 |
22 |
Correct |
23 ms |
24044 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
477 ms |
89140 KB |
n=1000000 |
2 |
Correct |
293 ms |
72268 KB |
n=666666 |
3 |
Correct |
169 ms |
49504 KB |
n=400000 |
4 |
Correct |
411 ms |
80972 KB |
n=285714 |
5 |
Correct |
32 ms |
24940 KB |
n=20000 |
6 |
Correct |
363 ms |
70304 KB |
n=181818 |
7 |
Correct |
19 ms |
24428 KB |
n=10000 |
8 |
Correct |
52 ms |
28524 KB |
n=6666 |
9 |
Correct |
18 ms |
24044 KB |
n=4000 |
10 |
Correct |
226 ms |
49132 KB |
n=2857 |
11 |
Correct |
17 ms |
24044 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23788 KB |
n=4 |
2 |
Correct |
17 ms |
23788 KB |
n=3 |
3 |
Correct |
17 ms |
23788 KB |
n=3 |
4 |
Correct |
18 ms |
23788 KB |
n=4 |
5 |
Correct |
18 ms |
23788 KB |
n=4 |
6 |
Correct |
17 ms |
23788 KB |
n=2 |
7 |
Correct |
17 ms |
23788 KB |
n=5 |
8 |
Correct |
17 ms |
23856 KB |
n=8 |
9 |
Correct |
16 ms |
23788 KB |
n=14 |
10 |
Correct |
18 ms |
23788 KB |
n=11 |
11 |
Correct |
44 ms |
28004 KB |
n=50000 |
12 |
Correct |
42 ms |
27880 KB |
n=50000 |
13 |
Correct |
19 ms |
23936 KB |
n=10 |
14 |
Correct |
20 ms |
23916 KB |
n=685 |
15 |
Correct |
20 ms |
23916 KB |
n=623 |
16 |
Correct |
20 ms |
23916 KB |
n=973 |
17 |
Correct |
22 ms |
23916 KB |
n=989 |
18 |
Correct |
18 ms |
23916 KB |
n=563 |
19 |
Correct |
22 ms |
24044 KB |
n=592 |
20 |
Correct |
19 ms |
24044 KB |
n=938 |
21 |
Correct |
17 ms |
24044 KB |
n=747 |
22 |
Correct |
23 ms |
24044 KB |
n=991 |
23 |
Correct |
477 ms |
89140 KB |
n=1000000 |
24 |
Correct |
293 ms |
72268 KB |
n=666666 |
25 |
Correct |
169 ms |
49504 KB |
n=400000 |
26 |
Correct |
411 ms |
80972 KB |
n=285714 |
27 |
Correct |
32 ms |
24940 KB |
n=20000 |
28 |
Correct |
363 ms |
70304 KB |
n=181818 |
29 |
Correct |
19 ms |
24428 KB |
n=10000 |
30 |
Correct |
52 ms |
28524 KB |
n=6666 |
31 |
Correct |
18 ms |
24044 KB |
n=4000 |
32 |
Correct |
226 ms |
49132 KB |
n=2857 |
33 |
Correct |
17 ms |
24044 KB |
n=2000 |
34 |
Correct |
33 ms |
26276 KB |
n=23514 |
35 |
Correct |
33 ms |
26276 KB |
n=23514 |
36 |
Correct |
17 ms |
23916 KB |
n=940 |
37 |
Correct |
16 ms |
23788 KB |
n=2 |
38 |
Correct |
116 ms |
38508 KB |
n=100000 |
39 |
Correct |
118 ms |
38508 KB |
n=100000 |
40 |
Correct |
16 ms |
23788 KB |
n=10 |
41 |
Correct |
17 ms |
23916 KB |
n=100 |
42 |
Correct |
22 ms |
24556 KB |
n=1000 |
43 |
Correct |
688 ms |
130592 KB |
n=1000000 |
44 |
Correct |
769 ms |
140956 KB |
n=1000000 |
45 |
Correct |
611 ms |
117028 KB |
n=666666 |
46 |
Correct |
481 ms |
91212 KB |
n=400000 |
47 |
Correct |
222 ms |
48832 KB |
n=2336 |
48 |
Correct |
412 ms |
80996 KB |
n=285714 |
49 |
Correct |
360 ms |
70268 KB |
n=181818 |
50 |
Correct |
252 ms |
54688 KB |
n=40000 |
51 |
Correct |
240 ms |
52388 KB |
n=20000 |
52 |
Correct |
228 ms |
50272 KB |
n=10000 |
53 |
Correct |
228 ms |
49900 KB |
n=6666 |
54 |
Correct |
222 ms |
49516 KB |
n=4000 |
55 |
Correct |
229 ms |
49132 KB |
n=2857 |
56 |
Correct |
213 ms |
48236 KB |
n=2000 |