#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int inf = INT_MAX;
const int mod = 998244353;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e6+5;
const int M = 1e2+1;
ll a[N],pos[N];
int main(){
boost;
int n,k;
cin>>n>>k;
ll sum=0,mx=0;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
mx=max(mx,a[i]);
}
ll part=sum/k;
if(sum%k||mx>part){
cout<<"-1";
return 0;
}
sum=0;
vector< vector<pli> >q;
vector<pli>v;
for(int i=1;i<=n;i++){
sum+=a[i];
if(sum>=part){
v.pb(mp(a[i]-(sum-part),i));
q.pb(v);
v.clear();
sum-=part;
if(sum)v.pb(mp(sum,i));
}
else v.pb(mp(a[i],i));
}
vector<ll>ans;
while(1){
ll mn=1e18;
for(int i=0;i<k;i++){
mn=min(mn,q[i][pos[i]].x);
}
//cout<<mn<<" ";
ans.pb(mn);
bool w=0;
for(int i=0;i<k;i++){
//cout<<q[i][pos[i]].y<<" ";
ans.pb(q[i][pos[i]].y);
q[i][pos[i]].x-=mn;
if(pos[i]<sz(q[i])&&!q[i][pos[i]].x){
pos[i]++;
if(pos[i]<sz(q[i]))w++;
}
}
if(!w)break;
}
cout<<sz(ans)/(k+1)<<endl;
for(int i=0;i<sz(ans);i++){
cout<<ans[i]<<" ";
if((i+1)%(k+1)==0)cout<<endl;
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:74:25: warning: use of an operand of type 'bool' in 'operator++' is deprecated [-Wdeprecated]
if(pos[i]<sz(q[i]))w++;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
1 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
1 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
1 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
1 ms |
376 KB |
n=11 |
11 |
Correct |
71 ms |
3760 KB |
n=50000 |
12 |
Correct |
67 ms |
3652 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
3 ms |
376 KB |
n=685 |
15 |
Correct |
3 ms |
376 KB |
n=623 |
16 |
Correct |
3 ms |
504 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
1 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
1 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
1 ms |
376 KB |
n=11 |
11 |
Correct |
71 ms |
3760 KB |
n=50000 |
12 |
Correct |
67 ms |
3652 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
3 ms |
376 KB |
n=685 |
15 |
Correct |
3 ms |
376 KB |
n=623 |
16 |
Correct |
3 ms |
504 KB |
n=973 |
17 |
Correct |
3 ms |
504 KB |
n=989 |
18 |
Correct |
3 ms |
504 KB |
n=563 |
19 |
Correct |
4 ms |
632 KB |
n=592 |
20 |
Correct |
4 ms |
632 KB |
n=938 |
21 |
Correct |
3 ms |
504 KB |
n=747 |
22 |
Correct |
4 ms |
632 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
975 ms |
57776 KB |
n=1000000 |
2 |
Correct |
489 ms |
34644 KB |
n=666666 |
3 |
Correct |
222 ms |
18788 KB |
n=400000 |
4 |
Correct |
644 ms |
42660 KB |
n=285714 |
5 |
Correct |
8 ms |
1272 KB |
n=20000 |
6 |
Correct |
481 ms |
37828 KB |
n=181818 |
7 |
Correct |
5 ms |
888 KB |
n=10000 |
8 |
Correct |
34 ms |
4960 KB |
n=6666 |
9 |
Correct |
3 ms |
632 KB |
n=4000 |
10 |
Correct |
169 ms |
25404 KB |
n=2857 |
11 |
Correct |
2 ms |
504 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
1 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
1 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
1 ms |
376 KB |
n=11 |
11 |
Correct |
71 ms |
3760 KB |
n=50000 |
12 |
Correct |
67 ms |
3652 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
3 ms |
376 KB |
n=685 |
15 |
Correct |
3 ms |
376 KB |
n=623 |
16 |
Correct |
3 ms |
504 KB |
n=973 |
17 |
Correct |
3 ms |
504 KB |
n=989 |
18 |
Correct |
3 ms |
504 KB |
n=563 |
19 |
Correct |
4 ms |
632 KB |
n=592 |
20 |
Correct |
4 ms |
632 KB |
n=938 |
21 |
Correct |
3 ms |
504 KB |
n=747 |
22 |
Correct |
4 ms |
632 KB |
n=991 |
23 |
Correct |
975 ms |
57776 KB |
n=1000000 |
24 |
Correct |
489 ms |
34644 KB |
n=666666 |
25 |
Correct |
222 ms |
18788 KB |
n=400000 |
26 |
Correct |
644 ms |
42660 KB |
n=285714 |
27 |
Correct |
8 ms |
1272 KB |
n=20000 |
28 |
Correct |
481 ms |
37828 KB |
n=181818 |
29 |
Correct |
5 ms |
888 KB |
n=10000 |
30 |
Correct |
34 ms |
4960 KB |
n=6666 |
31 |
Correct |
3 ms |
632 KB |
n=4000 |
32 |
Correct |
169 ms |
25404 KB |
n=2857 |
33 |
Correct |
2 ms |
504 KB |
n=2000 |
34 |
Correct |
41 ms |
2288 KB |
n=23514 |
35 |
Correct |
42 ms |
2288 KB |
n=23514 |
36 |
Correct |
3 ms |
504 KB |
n=940 |
37 |
Correct |
2 ms |
380 KB |
n=2 |
38 |
Correct |
194 ms |
11360 KB |
n=100000 |
39 |
Correct |
193 ms |
11364 KB |
n=100000 |
40 |
Correct |
3 ms |
376 KB |
n=10 |
41 |
Correct |
3 ms |
504 KB |
n=100 |
42 |
Correct |
7 ms |
1140 KB |
n=1000 |
43 |
Correct |
1705 ms |
79868 KB |
n=1000000 |
44 |
Correct |
1757 ms |
93832 KB |
n=1000000 |
45 |
Correct |
1251 ms |
70772 KB |
n=666666 |
46 |
Correct |
822 ms |
52896 KB |
n=400000 |
47 |
Correct |
173 ms |
25300 KB |
n=2336 |
48 |
Correct |
645 ms |
46636 KB |
n=285714 |
49 |
Correct |
487 ms |
40388 KB |
n=181818 |
50 |
Correct |
235 ms |
29524 KB |
n=40000 |
51 |
Correct |
206 ms |
27840 KB |
n=20000 |
52 |
Correct |
185 ms |
26312 KB |
n=10000 |
53 |
Correct |
184 ms |
26016 KB |
n=6666 |
54 |
Correct |
172 ms |
25532 KB |
n=4000 |
55 |
Correct |
175 ms |
25540 KB |
n=2857 |
56 |
Correct |
168 ms |
24484 KB |
n=2000 |