//Dost SEFEROĞLU
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 5e2+1,inf = 1e16,MOD = 1e9+7;
void solve() {
int n,k;
cin >> n >> k;
int s = 0;
set<pii,greater<pii>> ms;
vi a(n+1);
for (int i=1;i<=n;i++) {
cin >> a[i];
s+=a[i];
ms.insert({a[i],i});
}
if (s%k != 0 || ms.rbegin()->ff > s/k) {
cout << -1 << endl;
return;
}
vector<pair<int,vi>> ops;
while (1) {
vi vv;
auto itt = ms.begin();
int ctr = 0;
for (;ctr != k;++itt) {
vv.push_back(itt->ss);
ctr++;
}
if (ms.size() == k) {
if (ms.begin()->ff != ms.rbegin()->ff) {
cout << -1 << '\n';
return;
}
ops.push_back({ms.begin()->ff,vv});
break;
}
int subt = (ms.begin()->ff-next(ms.begin(),k)->ff);
if (!subt) {
ops.push_back({ms.begin()->ff,vv});
for (auto it : vv) {
ms.erase({a[it],it});
a[it]-=subt;
}
continue;
}
for (auto it : vv) {
ms.erase({a[it],it});
if (a[it]-subt > 0) ms.insert({a[it]-subt,it});
a[it]-=subt;
}
ops.push_back({subt,vv});
}
cout << ops.size() << '\n';
for (auto it : ops) {
cout << it.ff << " ";
for (auto itt : it.ss) cout << itt << " ";
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message
nicegift.cpp: In function 'void solve()':
nicegift.cpp:41:23: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int>, std::greater<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
41 | if (ms.size() == k) {
| ~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
500 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
460 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
500 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
460 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Incorrect |
1 ms |
336 KB |
Taken too much stones from the heap |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
500 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
460 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Incorrect |
1 ms |
336 KB |
Taken too much stones from the heap |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1285 ms |
100948 KB |
n=1000000 |
2 |
Correct |
684 ms |
63560 KB |
n=666666 |
3 |
Correct |
320 ms |
36936 KB |
n=400000 |
4 |
Execution timed out |
2053 ms |
26044 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
336 KB |
n=3 |
3 |
Correct |
1 ms |
500 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
460 KB |
n=4 |
6 |
Correct |
1 ms |
336 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Incorrect |
1 ms |
336 KB |
Taken too much stones from the heap |
9 |
Halted |
0 ms |
0 KB |
- |