#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long
#define int ll
#define forn(i, a, b) for(int i = (a); i <= (b); ++i)
#define forev(i, b, a) for(int i = (b); i >= (a); --i)
#define VAR(v, i) __typeof( i) v=(i)
#define forit(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;
const int maxn = (int)1e6 + 10;
const int mod = (int)1e9 + 7;
const int P = (int) 1e6 + 7;
const double pi = acos(-1.0);
#define inf mod
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> Vll;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;
typedef array<int, 3> triple;
int n, k, sum, mx;
pii a[maxn];
set<pii, greater<pii>> s;
inline void solve(){
scanf("%lld%lld", &n, &k);
forn(i, 1, n) {
scanf("%lld", &a[i].f);
a[i].s = i;
sum += a[i].f;
mx = max(mx, a[i].f);
s.insert(a[i]);
}
if(sum % k || sum / k < mx){
puts("-1");
return;
}
vector<pair<int, vi> > path;
while(1){
set<pii, greater<pii>> now = s;
int l = 1, r, res = -1;
forn(i, 1, k) {
r = now.begin() -> f;
now.erase(now.begin());
}
int f = s.begin() -> f;
if(!f) break;
int nmx;
if(sz(now)) nmx = now.begin() -> f;
else nmx = 0;
res = min(r, (sum - f) / (k - 1));
assert(res != -1);
vi cur;
vpii toAdd;
forn(i, 1, k){
cur.pb(s.begin() -> s);
toAdd.pb(*s.begin());
s.erase(s.begin());
}
for(auto x : toAdd)
s.insert({x.f - res, x.s});
sum -= res * k;
path.pb({res, cur});
}
printf("%lld\n", sz(path));
for(auto x : path){
printf("%lld ", x.f);
for(auto it : x.s) printf("%lld ", it);
puts("");
}
}
main () {
int t = 1;
//scanf("%d", &t);
while(t--)
solve();
}
Compilation message
nicegift.cpp: In function 'void solve()':
nicegift.cpp:59:9: warning: unused variable 'l' [-Wunused-variable]
59 | int l = 1, r, res = -1;
| ^
nicegift.cpp:69:9: warning: variable 'nmx' set but not used [-Wunused-but-set-variable]
69 | int nmx;
| ^~~
nicegift.cpp: At global scope:
nicegift.cpp:101:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main () {
| ^
nicegift.cpp: In function 'void solve()':
nicegift.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
40 | scanf("%lld%lld", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
nicegift.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
43 | scanf("%lld", &a[i].f);
| ~~~~~^~~~~~~~~~~~~~~~~
nicegift.cpp:59:16: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
59 | int l = 1, r, res = -1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Correct |
1 ms |
364 KB |
n=4 |
5 |
Correct |
1 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Correct |
1 ms |
364 KB |
n=4 |
5 |
Correct |
1 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
1 ms |
364 KB |
n=5 |
8 |
Correct |
1 ms |
364 KB |
n=8 |
9 |
Execution timed out |
2103 ms |
197432 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Correct |
1 ms |
364 KB |
n=4 |
5 |
Correct |
1 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
1 ms |
364 KB |
n=5 |
8 |
Correct |
1 ms |
364 KB |
n=8 |
9 |
Execution timed out |
2103 ms |
197432 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2104 ms |
141164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
1 ms |
364 KB |
n=3 |
4 |
Correct |
1 ms |
364 KB |
n=4 |
5 |
Correct |
1 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
1 ms |
364 KB |
n=5 |
8 |
Correct |
1 ms |
364 KB |
n=8 |
9 |
Execution timed out |
2103 ms |
197432 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |