#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
#define pb push_back
#define p push
using namespace std;
int n,k,q,tmp,sum=0,m=INT_MIN;
vector<int> v,pv;
vector<vector<pair<int,int>>> matrix;
vector<vector<int>> ans;
signed main(){
//freopen("input.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
q=n;
while(q--){
cin>>tmp;
sum+=tmp;
m=max(m,tmp);
v.pb(tmp);
}
if(float(sum/2)<float(m) || sum%k!=0){
cout<<-1;
return 0;
}
//pv = v;
const int flag = sum/k;
sum = 0;
int i = 0, j = 0;
stack<int> s;
//cout<<"t"<<endl;
while(i!=v.size()){
//cout<<"t1"<<endl;
matrix.pb({});
if(!s.empty()){
matrix[j].pb({s.top(),i-1});
s.pop();
}
while(sum<flag && i!=v.size()){
//cout<<"t2"<<endl;
matrix[j].pb({v[i],i});
//cout<<"t3"<<endl;
sum+=v[i];
i++;
}
if(sum-flag != 0){
//cout<<"sum - "<<sum<<endl;
//cout<<"matrix last before - "<<matrix[j][matrix[j].size()-1]<<endl;
matrix[j][matrix[j].size()-1].f+=(flag - sum);
s.p(sum-flag);
//cout<<flag<<endl;
//cout<<"matrix last - "<<matrix[j][matrix[j].size()-1]<<endl;
//cout<<"s.top() - "<<s.top()<<endl;
}
j++;
sum=sum-flag;
}
/*
for(int i=0;i<matrix.size();i++){
for(int j=0;j<matrix[i].size();j++){
cout<<matrix[i][j].f<<" "<<matrix[i][j].s<<endl;
}
cout<<endl;
}
*/
int z=0;
while(true){
sum = 0;
m = INT_MAX;
for(int i=0;i<matrix.size();i++){
//cout<<matrix[i][matrix[i].size()-1].f<<" ";
if(matrix[i].size()>0){
m=min(matrix[i][matrix[i].size()-1].f,m);
}else{
sum++;
}
}
if(sum==k){
break;
}else{
sum=0;
}
//cout<<endl<<m;
while(!s.empty()){
s.pop();
}
for(int i=0;i<matrix.size();i++){
s.p(matrix[i][matrix[i].size()-1].s+1);
matrix[i][matrix[i].size()-1].f-=m;
if(matrix[i][matrix[i].size()-1].f == 0){
matrix[i].pop_back();
}
}
ans.pb({});
ans[z].pb(m);
while(!s.empty()){
ans[z].pb(s.top());
s.pop();
}
z++;
}
if(ans.size() * k > 3000000){
cout<<-1;
return 0;
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
for(int j=0;j<ans[i].size();j++){
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:39:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | while(i!=v.size()){
| ~^~~~~~~~~~
nicegift.cpp:46:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | while(sum<flag && i!=v.size()){
| ~^~~~~~~~~~
nicegift.cpp:78:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i=0;i<matrix.size();i++){
| ~^~~~~~~~~~~~~~
nicegift.cpp:95:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0;i<matrix.size();i++){
| ~^~~~~~~~~~~~~~
nicegift.cpp:116:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(int i=0;i<ans.size();i++){
| ~^~~~~~~~~~~
nicegift.cpp:117:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int j=0;j<ans[i].size();j++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
224 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
224 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
204 KB |
n=8 |
9 |
Correct |
1 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
66 ms |
5120 KB |
n=50000 |
12 |
Correct |
63 ms |
4932 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
2 ms |
332 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
224 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
204 KB |
n=8 |
9 |
Correct |
1 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
66 ms |
5120 KB |
n=50000 |
12 |
Correct |
63 ms |
4932 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
2 ms |
332 KB |
n=973 |
17 |
Correct |
2 ms |
332 KB |
n=989 |
18 |
Correct |
2 ms |
332 KB |
n=563 |
19 |
Correct |
2 ms |
460 KB |
n=592 |
20 |
Correct |
3 ms |
460 KB |
n=938 |
21 |
Correct |
3 ms |
460 KB |
n=747 |
22 |
Correct |
3 ms |
460 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1208 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
1 ms |
224 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
204 KB |
n=8 |
9 |
Correct |
1 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
66 ms |
5120 KB |
n=50000 |
12 |
Correct |
63 ms |
4932 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
2 ms |
332 KB |
n=973 |
17 |
Correct |
2 ms |
332 KB |
n=989 |
18 |
Correct |
2 ms |
332 KB |
n=563 |
19 |
Correct |
2 ms |
460 KB |
n=592 |
20 |
Correct |
3 ms |
460 KB |
n=938 |
21 |
Correct |
3 ms |
460 KB |
n=747 |
22 |
Correct |
3 ms |
460 KB |
n=991 |
23 |
Runtime error |
1208 ms |
524292 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |