#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
using pii=pair<int,int>;
const int lim=1e5+100;
struct op{
int val;
vector<int>inds;
};
signed main(){
int n,k;
cin>>n>>k;
int a[n],sm=0;
for(int i=0;i<n;i++)cin>>a[i],sm+=a[i];
int p[n];
iota(p,p+n,0);
sort(p,p+n,[&](int i,int j)->bool {
return a[i]>a[j];
});
if(sm%k){
cout<<-1;
return 0;
}
sm/=k;
if(sm<a[p[0]]){
cout<<-1;
return 0;
}
vector<op>ops;
int left=n;
while(left&&a[p[0]]!=a[p[left-1]]){
op todo;
int last=0;
for(;last<n;last++){
if(a[p[last]]==sm){
todo.inds.pb(p[last]);
}else break;
}
todo.val=min(a[p[left-1]],sm-a[p[last]]);
for(int i=left-1;todo.inds.size()<k;i--){
todo.inds.pb(p[i]);
}
sm-=todo.val;
for(int i:todo.inds){
a[i]-=todo.val;
}
ops.pb(todo);
while(left&&a[p[left-1]]==0)left--;
}
if(left){
int z=left/gcd(left,k);
int val=sm/z;
int i=0;
for(int t=0;t<z;t++){
op todo;
todo.val=val;
for(int j=0;j<k;j++,(i=((i+1)%left))){
todo.inds.pb(p[i]);
}
ops.pb(todo);
}
}
cout<<ops.size()<<'\n';
for(op&pp:ops){
cout<<pp.val<<' ';
for(int i:pp.inds){
cout<<i+1<<' ';
}
cout<<'\n';
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:43:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
43 | for(int i=left-1;todo.inds.size()<k;i--){
| ~~~~~~~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
17 ms |
3524 KB |
n=50000 |
12 |
Correct |
15 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
336 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
17 ms |
3524 KB |
n=50000 |
12 |
Correct |
15 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
336 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
17 |
Correct |
1 ms |
336 KB |
n=989 |
18 |
Correct |
2 ms |
564 KB |
n=563 |
19 |
Correct |
2 ms |
336 KB |
n=592 |
20 |
Correct |
2 ms |
592 KB |
n=938 |
21 |
Correct |
1 ms |
336 KB |
n=747 |
22 |
Correct |
2 ms |
592 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
529 ms |
62368 KB |
n=1000000 |
2 |
Correct |
317 ms |
33760 KB |
n=666666 |
3 |
Correct |
188 ms |
18256 KB |
n=400000 |
4 |
Correct |
296 ms |
50336 KB |
n=285714 |
5 |
Correct |
10 ms |
1104 KB |
n=20000 |
6 |
Correct |
245 ms |
44176 KB |
n=181818 |
7 |
Correct |
5 ms |
848 KB |
n=10000 |
8 |
Correct |
24 ms |
4708 KB |
n=6666 |
9 |
Correct |
3 ms |
592 KB |
n=4000 |
10 |
Correct |
143 ms |
25572 KB |
n=2857 |
11 |
Correct |
2 ms |
336 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
17 ms |
3524 KB |
n=50000 |
12 |
Correct |
15 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
336 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
17 |
Correct |
1 ms |
336 KB |
n=989 |
18 |
Correct |
2 ms |
564 KB |
n=563 |
19 |
Correct |
2 ms |
336 KB |
n=592 |
20 |
Correct |
2 ms |
592 KB |
n=938 |
21 |
Correct |
1 ms |
336 KB |
n=747 |
22 |
Correct |
2 ms |
592 KB |
n=991 |
23 |
Correct |
529 ms |
62368 KB |
n=1000000 |
24 |
Correct |
317 ms |
33760 KB |
n=666666 |
25 |
Correct |
188 ms |
18256 KB |
n=400000 |
26 |
Correct |
296 ms |
50336 KB |
n=285714 |
27 |
Correct |
10 ms |
1104 KB |
n=20000 |
28 |
Correct |
245 ms |
44176 KB |
n=181818 |
29 |
Correct |
5 ms |
848 KB |
n=10000 |
30 |
Correct |
24 ms |
4708 KB |
n=6666 |
31 |
Correct |
3 ms |
592 KB |
n=4000 |
32 |
Correct |
143 ms |
25572 KB |
n=2857 |
33 |
Correct |
2 ms |
336 KB |
n=2000 |
34 |
Correct |
13 ms |
2524 KB |
n=23514 |
35 |
Correct |
13 ms |
2500 KB |
n=23514 |
36 |
Correct |
1 ms |
336 KB |
n=940 |
37 |
Correct |
1 ms |
504 KB |
n=2 |
38 |
Correct |
70 ms |
14508 KB |
n=100000 |
39 |
Correct |
79 ms |
14480 KB |
n=100000 |
40 |
Correct |
1 ms |
336 KB |
n=10 |
41 |
Correct |
1 ms |
336 KB |
n=100 |
42 |
Correct |
4 ms |
1104 KB |
n=1000 |
43 |
Correct |
527 ms |
101784 KB |
n=1000000 |
44 |
Correct |
911 ms |
113236 KB |
n=1000000 |
45 |
Correct |
593 ms |
83660 KB |
n=666666 |
46 |
Correct |
379 ms |
60252 KB |
n=400000 |
47 |
Correct |
135 ms |
25204 KB |
n=2336 |
48 |
Correct |
342 ms |
52380 KB |
n=285714 |
49 |
Correct |
276 ms |
42408 KB |
n=181818 |
50 |
Correct |
171 ms |
31440 KB |
n=40000 |
51 |
Correct |
164 ms |
28604 KB |
n=20000 |
52 |
Correct |
144 ms |
26560 KB |
n=10000 |
53 |
Correct |
150 ms |
26308 KB |
n=6666 |
54 |
Correct |
137 ms |
25416 KB |
n=4000 |
55 |
Correct |
144 ms |
25472 KB |
n=2857 |
56 |
Correct |
72 ms |
12732 KB |
n=2000 |