#include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pb push_back
#define pii pair <int, int>
using namespace std;
const int N = 3e6 + 5;
int n,k,a[N],mx,cur,sum,le,ri,mid,sum1,mx1,mn,anss,cnt;
vector < pii > v;
vector <int> ans[N];
vector < pii > v1;
int pas[N];
set <pii > :: iterator it;
set < pii > ms;
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
for (int i = 1; i <= n; i++) {
cin>>a[i];
mx = max(mx,a[i]);
sum += a[i];
ms.insert({a[i],i});
}
if (mx > sum / k || sum % k) {
cout<<"-1"<<endl;
return 0;
}
int ss = 0;
sum1 = sum;
while (true) {
v.clear();v1.clear();
mx = mx1 = 0;
if (ms.size() == 0) break;
mn = 1e18;
it = ms.end();
cnt = 0;
while (true) {
it--;
cnt++;
if (cnt <= k) {
mx = max(mx,(*it).f);
mn = min(mn,(*it).f);
}
if (cnt == k + 1) {
mx1 = (*it).f;
break;
}
if (it == ms.begin()) break;
}
// anss <= mn da
anss = mn;
int ff = (sum1 - k * mx1)/k;
if (mx1 >= mx - ff)
anss = min(anss,ff);
/*
le = 1;
ri = 1e18;
while(le <= ri) {
mid = (le + ri) / 2;
if (mn < mid || max(mx1,mx-mid)>(sum1 - k*mid)/k) {
ri = mid - 1;
} else {
anss = mid;
le = mid + 1;
}
}*/
cur++;
pas[cur] = anss;
for (int i = 0; i < k; i++) {
it = ms.end();
it--;
int x = (*it).f; int y = (*it).s;
a[y] -= anss;sum1 -= anss;
v1.pb({x - anss, y});
ans[cur].pb(y);
ms.erase(it);
}
for (int i = 0; i < v1.size(); i++) {
if (v1[i].f != 0)
ms.insert({v1[i].f,v1[i].s});
}
}
cout<<cur<<"\n";
for (int i = 1; i <= cur; i++) {
cout<<pas[i]<<" ";
for (int j = 0; j < ans[i].size(); j++) {
cout<<ans[i][j]<<" ";
}cout<<"\n";
}
}
Compilation message
nicegift.cpp:16:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
16 | main() {
| ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:79:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < v1.size(); i++) {
| ~~^~~~~~~~~~~
nicegift.cpp:87:27: 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]
87 | for (int j = 0; j < ans[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
nicegift.cpp:29:9: warning: unused variable 'ss' [-Wunused-variable]
29 | int ss = 0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70644 KB |
n=4 |
2 |
Correct |
42 ms |
70716 KB |
n=3 |
3 |
Correct |
38 ms |
70732 KB |
n=3 |
4 |
Correct |
37 ms |
70732 KB |
n=4 |
5 |
Correct |
37 ms |
70696 KB |
n=4 |
6 |
Correct |
36 ms |
70712 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70644 KB |
n=4 |
2 |
Correct |
42 ms |
70716 KB |
n=3 |
3 |
Correct |
38 ms |
70732 KB |
n=3 |
4 |
Correct |
37 ms |
70732 KB |
n=4 |
5 |
Correct |
37 ms |
70696 KB |
n=4 |
6 |
Correct |
36 ms |
70712 KB |
n=2 |
7 |
Correct |
39 ms |
70728 KB |
n=5 |
8 |
Correct |
43 ms |
70628 KB |
n=8 |
9 |
Correct |
49 ms |
70752 KB |
n=14 |
10 |
Correct |
42 ms |
70740 KB |
n=11 |
11 |
Correct |
67 ms |
74964 KB |
n=50000 |
12 |
Correct |
70 ms |
74812 KB |
n=50000 |
13 |
Correct |
38 ms |
70752 KB |
n=10 |
14 |
Correct |
50 ms |
70820 KB |
n=685 |
15 |
Correct |
46 ms |
70724 KB |
n=623 |
16 |
Correct |
42 ms |
70728 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70644 KB |
n=4 |
2 |
Correct |
42 ms |
70716 KB |
n=3 |
3 |
Correct |
38 ms |
70732 KB |
n=3 |
4 |
Correct |
37 ms |
70732 KB |
n=4 |
5 |
Correct |
37 ms |
70696 KB |
n=4 |
6 |
Correct |
36 ms |
70712 KB |
n=2 |
7 |
Correct |
39 ms |
70728 KB |
n=5 |
8 |
Correct |
43 ms |
70628 KB |
n=8 |
9 |
Correct |
49 ms |
70752 KB |
n=14 |
10 |
Correct |
42 ms |
70740 KB |
n=11 |
11 |
Correct |
67 ms |
74964 KB |
n=50000 |
12 |
Correct |
70 ms |
74812 KB |
n=50000 |
13 |
Correct |
38 ms |
70752 KB |
n=10 |
14 |
Correct |
50 ms |
70820 KB |
n=685 |
15 |
Correct |
46 ms |
70724 KB |
n=623 |
16 |
Correct |
42 ms |
70728 KB |
n=973 |
17 |
Correct |
38 ms |
70836 KB |
n=989 |
18 |
Correct |
38 ms |
70736 KB |
n=563 |
19 |
Correct |
39 ms |
70764 KB |
n=592 |
20 |
Correct |
44 ms |
70812 KB |
n=938 |
21 |
Correct |
38 ms |
70836 KB |
n=747 |
22 |
Correct |
40 ms |
70848 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
652 ms |
162004 KB |
n=1000000 |
2 |
Correct |
417 ms |
128644 KB |
n=666666 |
3 |
Correct |
282 ms |
104652 KB |
n=400000 |
4 |
Correct |
188 ms |
94864 KB |
n=285714 |
5 |
Correct |
54 ms |
72536 KB |
n=20000 |
6 |
Correct |
133 ms |
86140 KB |
n=181818 |
7 |
Correct |
51 ms |
71664 KB |
n=10000 |
8 |
Correct |
56 ms |
71344 KB |
n=6666 |
9 |
Correct |
41 ms |
71080 KB |
n=4000 |
10 |
Correct |
41 ms |
71140 KB |
n=2857 |
11 |
Correct |
48 ms |
70852 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
70644 KB |
n=4 |
2 |
Correct |
42 ms |
70716 KB |
n=3 |
3 |
Correct |
38 ms |
70732 KB |
n=3 |
4 |
Correct |
37 ms |
70732 KB |
n=4 |
5 |
Correct |
37 ms |
70696 KB |
n=4 |
6 |
Correct |
36 ms |
70712 KB |
n=2 |
7 |
Correct |
39 ms |
70728 KB |
n=5 |
8 |
Correct |
43 ms |
70628 KB |
n=8 |
9 |
Correct |
49 ms |
70752 KB |
n=14 |
10 |
Correct |
42 ms |
70740 KB |
n=11 |
11 |
Correct |
67 ms |
74964 KB |
n=50000 |
12 |
Correct |
70 ms |
74812 KB |
n=50000 |
13 |
Correct |
38 ms |
70752 KB |
n=10 |
14 |
Correct |
50 ms |
70820 KB |
n=685 |
15 |
Correct |
46 ms |
70724 KB |
n=623 |
16 |
Correct |
42 ms |
70728 KB |
n=973 |
17 |
Correct |
38 ms |
70836 KB |
n=989 |
18 |
Correct |
38 ms |
70736 KB |
n=563 |
19 |
Correct |
39 ms |
70764 KB |
n=592 |
20 |
Correct |
44 ms |
70812 KB |
n=938 |
21 |
Correct |
38 ms |
70836 KB |
n=747 |
22 |
Correct |
40 ms |
70848 KB |
n=991 |
23 |
Correct |
652 ms |
162004 KB |
n=1000000 |
24 |
Correct |
417 ms |
128644 KB |
n=666666 |
25 |
Correct |
282 ms |
104652 KB |
n=400000 |
26 |
Correct |
188 ms |
94864 KB |
n=285714 |
27 |
Correct |
54 ms |
72536 KB |
n=20000 |
28 |
Correct |
133 ms |
86140 KB |
n=181818 |
29 |
Correct |
51 ms |
71664 KB |
n=10000 |
30 |
Correct |
56 ms |
71344 KB |
n=6666 |
31 |
Correct |
41 ms |
71080 KB |
n=4000 |
32 |
Correct |
41 ms |
71140 KB |
n=2857 |
33 |
Correct |
48 ms |
70852 KB |
n=2000 |
34 |
Correct |
55 ms |
72928 KB |
n=23514 |
35 |
Correct |
53 ms |
73044 KB |
n=23514 |
36 |
Correct |
40 ms |
70748 KB |
n=940 |
37 |
Correct |
39 ms |
70752 KB |
n=2 |
38 |
Correct |
93 ms |
79164 KB |
n=100000 |
39 |
Correct |
88 ms |
79216 KB |
n=100000 |
40 |
Correct |
40 ms |
70764 KB |
n=10 |
41 |
Correct |
39 ms |
70800 KB |
n=100 |
42 |
Correct |
48 ms |
70936 KB |
n=1000 |
43 |
Correct |
815 ms |
163400 KB |
n=1000000 |
44 |
Execution timed out |
2107 ms |
165544 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |