#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll n,k,a[2000005],s,las,p,cur,maxi,l,r,mid,can;
set<pair<ll,ll> >st;
vector<vector<ll> >ans;
vector<ll>g;
int main(){
ios::sync_with_stdio(0);
cin>>n>>k;
bool ok = 1;
for(int i=1; i<=n; i++){
cin>>a[i];
if(a[i] != a[1])ok = 0;
s += a[i];
st.insert(mp(-a[i] , i));
}
if(s % k){
cout << "-1";
return 0;
}
ll raod = s / k;
ll sum = s;
ll ra = 0;
while(sum){
if((int)st.size() < k){
cout << "-1";
return 0;
}
p = 0,cur = 0;
g.clear();
maxi = -(*st.begin()).f;
for(set<pair<ll,ll> >::iterator it = st.begin(); it != st.end(); it++){
p++;
if(p > k){
cur = (*it).s;
break;
}
las = (*it).s;
}
l = 1 , r = a[las] , mid , can = -1;
while(r >= l){
mid = (l + r) / 2;
if(max(maxi - mid , a[cur]) * k <= sum - mid * k){
l = mid + 1;
can = mid;
}
else {
r = mid - 1;
}
}
if(can <= 0){
cout << "-1";
return 0;
}
sum -= can * k;
ra += k;
p = 0;
g.pb(can);
for(set<pair<ll,ll> >::iterator it = st.begin(); it != st.end(); it++){
p++;
if(p > k){
cur = (*it).s;
break;
}
g.pb((*it).s);
}
for(int i=1; i<g.size(); i++){
st.erase(mp(-a[g[i]] , g[i]));
a[g[i]]-=can;
if(a[g[i]])st.insert(mp(-a[g[i]] , g[i]));
}
ans.pb(g);
}
cout<<ans.size()<<"\n";
for(int i=0; i<ans.size(); i++)
for(int j=0; j<ans[i].size(); j++)
cout<<ans[i][j]<<" ";
return 0;
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:47:43: warning: right operand of comma operator has no effect [-Wunused-value]
l = 1 , r = a[las] , mid , can = -1;
^
nicegift.cpp:74:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1; i<g.size(); i++){
~^~~~~~~~~
nicegift.cpp:84:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<ans.size(); i++)
~^~~~~~~~~~~
nicegift.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<ans[i].size(); j++)
~^~~~~~~~~~~~~~
nicegift.cpp:15:8: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
bool ok = 1;
^~
nicegift.cpp:28:8: warning: unused variable 'raod' [-Wunused-variable]
ll raod = s / k;
^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
2 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 |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
352 KB |
n=11 |
11 |
Correct |
37 ms |
5168 KB |
n=50000 |
12 |
Correct |
32 ms |
4904 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
504 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 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 |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
352 KB |
n=11 |
11 |
Correct |
37 ms |
5168 KB |
n=50000 |
12 |
Correct |
32 ms |
4904 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
504 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 ms |
504 KB |
n=973 |
17 |
Correct |
2 ms |
504 KB |
n=989 |
18 |
Correct |
2 ms |
376 KB |
n=563 |
19 |
Correct |
3 ms |
424 KB |
n=592 |
20 |
Correct |
3 ms |
376 KB |
n=938 |
21 |
Correct |
2 ms |
380 KB |
n=747 |
22 |
Correct |
3 ms |
504 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1046 ms |
84792 KB |
n=1000000 |
2 |
Correct |
653 ms |
55096 KB |
n=666666 |
3 |
Correct |
374 ms |
34404 KB |
n=400000 |
4 |
Correct |
250 ms |
22996 KB |
n=285714 |
5 |
Correct |
15 ms |
1912 KB |
n=20000 |
6 |
Correct |
152 ms |
14556 KB |
n=181818 |
7 |
Correct |
8 ms |
1144 KB |
n=10000 |
8 |
Correct |
7 ms |
888 KB |
n=6666 |
9 |
Correct |
4 ms |
632 KB |
n=4000 |
10 |
Correct |
8 ms |
632 KB |
n=2857 |
11 |
Correct |
3 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 |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
352 KB |
n=11 |
11 |
Correct |
37 ms |
5168 KB |
n=50000 |
12 |
Correct |
32 ms |
4904 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
504 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 ms |
504 KB |
n=973 |
17 |
Correct |
2 ms |
504 KB |
n=989 |
18 |
Correct |
2 ms |
376 KB |
n=563 |
19 |
Correct |
3 ms |
424 KB |
n=592 |
20 |
Correct |
3 ms |
376 KB |
n=938 |
21 |
Correct |
2 ms |
380 KB |
n=747 |
22 |
Correct |
3 ms |
504 KB |
n=991 |
23 |
Correct |
1046 ms |
84792 KB |
n=1000000 |
24 |
Correct |
653 ms |
55096 KB |
n=666666 |
25 |
Correct |
374 ms |
34404 KB |
n=400000 |
26 |
Correct |
250 ms |
22996 KB |
n=285714 |
27 |
Correct |
15 ms |
1912 KB |
n=20000 |
28 |
Correct |
152 ms |
14556 KB |
n=181818 |
29 |
Correct |
8 ms |
1144 KB |
n=10000 |
30 |
Correct |
7 ms |
888 KB |
n=6666 |
31 |
Correct |
4 ms |
632 KB |
n=4000 |
32 |
Correct |
8 ms |
632 KB |
n=2857 |
33 |
Correct |
3 ms |
504 KB |
n=2000 |
34 |
Correct |
28 ms |
2932 KB |
n=23514 |
35 |
Correct |
27 ms |
2932 KB |
n=23514 |
36 |
Correct |
3 ms |
504 KB |
n=940 |
37 |
Correct |
2 ms |
380 KB |
n=2 |
38 |
Correct |
73 ms |
8732 KB |
n=100000 |
39 |
Correct |
71 ms |
8688 KB |
n=100000 |
40 |
Correct |
2 ms |
376 KB |
n=10 |
41 |
Correct |
3 ms |
376 KB |
n=100 |
42 |
Correct |
6 ms |
504 KB |
n=1000 |
43 |
Correct |
1373 ms |
103108 KB |
n=1000000 |
44 |
Execution timed out |
2073 ms |
83404 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |