#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;
for(int i=0;i<n;i++){
int p;cin>>p;
total+=p;
m.insert({p,i+1});
}
if(total%k!=0){
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);
}
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:23:19: 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]
23 | while(m.size()>k+1){
| ~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
1 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
1 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
83 ms |
5380 KB |
n=50000 |
12 |
Correct |
85 ms |
5376 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
1 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
83 ms |
5380 KB |
n=50000 |
12 |
Correct |
85 ms |
5376 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
17 |
Execution timed out |
2077 ms |
515516 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1819 ms |
109180 KB |
n=1000000 |
2 |
Correct |
1564 ms |
77224 KB |
n=666666 |
3 |
Correct |
1000 ms |
50864 KB |
n=400000 |
4 |
Incorrect |
382 ms |
39868 KB |
Jury has the answer but participant has not |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
1 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
83 ms |
5380 KB |
n=50000 |
12 |
Correct |
85 ms |
5376 KB |
n=50000 |
13 |
Correct |
1 ms |
348 KB |
n=10 |
14 |
Correct |
2 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
2 ms |
348 KB |
n=973 |
17 |
Execution timed out |
2077 ms |
515516 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |