#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define fi first
#define se second
const int N = 1000010;
using namespace std;
int n;
int k;
long long a[N];
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
set < pair < long long, int > > s;
long long S = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
s.insert({a[i], i});
S += a[i];
}
if(1ll * (--s.end())->fi > S / k || S % k){
cout << -1 << "\n";
return 0;
}
vector < long long > qu;
vector < vector < int > > res;
vector < int > v;
while(s.size() > 0){
v.clear();
for(int i = 0; i < k; i++){
v.push_back((--s.end())->se);
s.erase(--s.end());
}
long long rem = 0;
if(!s.empty()){
rem = (--s.end())->fi;
}
long long l = 1, r = min(a[v.back()], S / k);
while(l < r){
long long m = (l + r) / 2 + 1;
if(max(rem, a[v[0]] - m) > (S - m * k) / k){
r = m - 1;
} else{
l = m;
}
}
S -= k * l;
res.push_back(v);
qu.push_back(l);
for(int i: v){
a[i] -= l;
if(!a[i]){
continue;
}
s.insert({a[i], i});
}
}
cout << qu.size() << "\n";
for(int i = 0; i < qu.size(); i++){
cout << qu[i];
for(int x: res[i]){
cout << " " << x;
}
cout << "\n";
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:72:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < qu.size(); i++){
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
376 KB |
n=11 |
11 |
Correct |
33 ms |
5280 KB |
n=50000 |
12 |
Correct |
29 ms |
4848 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 |
3 ms |
376 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 |
376 KB |
n=11 |
11 |
Correct |
33 ms |
5280 KB |
n=50000 |
12 |
Correct |
29 ms |
4848 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 |
3 ms |
376 KB |
n=973 |
17 |
Correct |
3 ms |
376 KB |
n=989 |
18 |
Correct |
3 ms |
376 KB |
n=563 |
19 |
Correct |
3 ms |
376 KB |
n=592 |
20 |
Correct |
3 ms |
504 KB |
n=938 |
21 |
Correct |
3 ms |
424 KB |
n=747 |
22 |
Correct |
2 ms |
376 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1403 ms |
84584 KB |
n=1000000 |
2 |
Correct |
810 ms |
64184 KB |
n=666666 |
3 |
Correct |
395 ms |
37880 KB |
n=400000 |
4 |
Correct |
260 ms |
26920 KB |
n=285714 |
5 |
Correct |
14 ms |
2112 KB |
n=20000 |
6 |
Correct |
152 ms |
17808 KB |
n=181818 |
7 |
Correct |
8 ms |
1272 KB |
n=10000 |
8 |
Correct |
7 ms |
1016 KB |
n=6666 |
9 |
Correct |
4 ms |
764 KB |
n=4000 |
10 |
Correct |
7 ms |
760 KB |
n=2857 |
11 |
Correct |
3 ms |
632 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 |
376 KB |
n=11 |
11 |
Correct |
33 ms |
5280 KB |
n=50000 |
12 |
Correct |
29 ms |
4848 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 |
3 ms |
376 KB |
n=973 |
17 |
Correct |
3 ms |
376 KB |
n=989 |
18 |
Correct |
3 ms |
376 KB |
n=563 |
19 |
Correct |
3 ms |
376 KB |
n=592 |
20 |
Correct |
3 ms |
504 KB |
n=938 |
21 |
Correct |
3 ms |
424 KB |
n=747 |
22 |
Correct |
2 ms |
376 KB |
n=991 |
23 |
Correct |
1403 ms |
84584 KB |
n=1000000 |
24 |
Correct |
810 ms |
64184 KB |
n=666666 |
25 |
Correct |
395 ms |
37880 KB |
n=400000 |
26 |
Correct |
260 ms |
26920 KB |
n=285714 |
27 |
Correct |
14 ms |
2112 KB |
n=20000 |
28 |
Correct |
152 ms |
17808 KB |
n=181818 |
29 |
Correct |
8 ms |
1272 KB |
n=10000 |
30 |
Correct |
7 ms |
1016 KB |
n=6666 |
31 |
Correct |
4 ms |
764 KB |
n=4000 |
32 |
Correct |
7 ms |
760 KB |
n=2857 |
33 |
Correct |
3 ms |
632 KB |
n=2000 |
34 |
Correct |
33 ms |
3272 KB |
n=23514 |
35 |
Correct |
33 ms |
3252 KB |
n=23514 |
36 |
Correct |
3 ms |
504 KB |
n=940 |
37 |
Correct |
2 ms |
376 KB |
n=2 |
38 |
Correct |
84 ms |
9500 KB |
n=100000 |
39 |
Correct |
81 ms |
9392 KB |
n=100000 |
40 |
Correct |
2 ms |
376 KB |
n=10 |
41 |
Correct |
3 ms |
376 KB |
n=100 |
42 |
Correct |
5 ms |
632 KB |
n=1000 |
43 |
Correct |
1386 ms |
123860 KB |
n=1000000 |
44 |
Execution timed out |
2043 ms |
93268 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |