#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LLA(x) x.rbegin(),x.rend()
#define print(x) for(auto el:x)cout<<el<<' ';cout<<"\n";
#define printPII(x) for(auto [a,b]:x)cout<<a<<","<<b<<' ';cout<<'\n';
using namespace std;
using ll = long long;
using PII = pair <ll, int>;
ll n, k, onePick = 1;
bool impossible;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> k;
vector <vector <int> > ans;
vector <ll> ansW;
set <PII> bfs;
ll sum = 0, val;
int id;
for (int i = 1; i <= n; i++) {
ll x; cin >> x;
sum += x;
bfs.insert({x, i});
}
bfs.insert({0, 0});
ll packs = sum / k;
if (sum % k != 0 || bfs.rbegin()->ff > packs) {
cout << "-1\n";
return 0;
}
while(packs > 0) {
vector <PII> add;
for (int i = 0; i < k; i++) {
auto it = --bfs.end();
tie(val, id) = *it;
bfs.erase(it);
add.pb({val, id});
}
ll pick = min(add.back().ff, packs-((--bfs.end())->ff));
packs -= pick;
vector <int> cur;
for (int i = 0; i < k; i++) {
tie(val, id) = add.back();
add.pop_back();
val -= pick;
cur.pb(id);
bfs.insert({val, id});
}
ansW.pb(pick);
ans.pb(cur);
}
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); i++){
cout << ansW[i] << ' ';
print(ans[i]);
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < ans.size(); i++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
308 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
36 ms |
5836 KB |
n=50000 |
12 |
Correct |
34 ms |
5436 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
308 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
308 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
308 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
36 ms |
5836 KB |
n=50000 |
12 |
Correct |
34 ms |
5436 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
308 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
308 KB |
n=973 |
17 |
Correct |
1 ms |
332 KB |
n=989 |
18 |
Correct |
1 ms |
308 KB |
n=563 |
19 |
Correct |
1 ms |
292 KB |
n=592 |
20 |
Correct |
1 ms |
308 KB |
n=938 |
21 |
Correct |
1 ms |
312 KB |
n=747 |
22 |
Correct |
1 ms |
332 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
936 ms |
108736 KB |
n=1000000 |
2 |
Correct |
546 ms |
65952 KB |
n=666666 |
3 |
Correct |
297 ms |
35748 KB |
n=400000 |
4 |
Correct |
207 ms |
24736 KB |
n=285714 |
5 |
Correct |
11 ms |
1996 KB |
n=20000 |
6 |
Correct |
123 ms |
15500 KB |
n=181818 |
7 |
Correct |
6 ms |
1072 KB |
n=10000 |
8 |
Correct |
5 ms |
816 KB |
n=6666 |
9 |
Correct |
4 ms |
572 KB |
n=4000 |
10 |
Correct |
5 ms |
716 KB |
n=2857 |
11 |
Correct |
2 ms |
452 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
n=4 |
2 |
Correct |
1 ms |
204 KB |
n=3 |
3 |
Correct |
1 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
0 ms |
308 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
36 ms |
5836 KB |
n=50000 |
12 |
Correct |
34 ms |
5436 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
1 ms |
308 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
308 KB |
n=973 |
17 |
Correct |
1 ms |
332 KB |
n=989 |
18 |
Correct |
1 ms |
308 KB |
n=563 |
19 |
Correct |
1 ms |
292 KB |
n=592 |
20 |
Correct |
1 ms |
308 KB |
n=938 |
21 |
Correct |
1 ms |
312 KB |
n=747 |
22 |
Correct |
1 ms |
332 KB |
n=991 |
23 |
Correct |
936 ms |
108736 KB |
n=1000000 |
24 |
Correct |
546 ms |
65952 KB |
n=666666 |
25 |
Correct |
297 ms |
35748 KB |
n=400000 |
26 |
Correct |
207 ms |
24736 KB |
n=285714 |
27 |
Correct |
11 ms |
1996 KB |
n=20000 |
28 |
Correct |
123 ms |
15500 KB |
n=181818 |
29 |
Correct |
6 ms |
1072 KB |
n=10000 |
30 |
Correct |
5 ms |
816 KB |
n=6666 |
31 |
Correct |
4 ms |
572 KB |
n=4000 |
32 |
Correct |
5 ms |
716 KB |
n=2857 |
33 |
Correct |
2 ms |
452 KB |
n=2000 |
34 |
Correct |
23 ms |
3324 KB |
n=23514 |
35 |
Correct |
23 ms |
3232 KB |
n=23514 |
36 |
Correct |
1 ms |
440 KB |
n=940 |
37 |
Correct |
0 ms |
312 KB |
n=2 |
38 |
Correct |
70 ms |
8884 KB |
n=100000 |
39 |
Correct |
72 ms |
8916 KB |
n=100000 |
40 |
Correct |
0 ms |
204 KB |
n=10 |
41 |
Correct |
1 ms |
204 KB |
n=100 |
42 |
Correct |
3 ms |
460 KB |
n=1000 |
43 |
Correct |
1043 ms |
126468 KB |
n=1000000 |
44 |
Execution timed out |
2009 ms |
95672 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |