This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 1000000
#define L 3000000
long long min(long long a, long long b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
long long aa[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N], iii[L];
static long long xx[N + 1];
int n, m, k, g, h, i, j;
long long sum, cnt, x;
scanf("%d%d", &n, &k);
sum = 0;
for (i = 0; i < n; i++) {
scanf("%lld", &aa[i]);
sum += aa[i];
}
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
if (sum % k != 0) {
printf("-1\n");
return 0;
}
cnt = sum / k;
if (aa[ii[n - 1]] > cnt) {
printf("-1\n");
return 0;
}
m = 0, i = 0, j = n - 1;
while (cnt > 0) {
while (i < n - k && aa[ii[i]] == 0)
i++;
while (j >= n - k && aa[ii[j]] == cnt)
j--;
if (i == n - k)
x = aa[ii[n - k]];
else
x = min(aa[ii[i]], cnt - aa[ii[j]]);
xx[m] = x;
for (h = i; h < i + k - (n - 1 - j); h++)
aa[iii[m * k + h - i] = ii[h]] -= x;
for (h = j + 1; h < n; h++)
aa[iii[m * k + k - n + h] = ii[h]] -= x;
cnt -= x;
m++;
}
printf("%d\n", m);
for (g = 0; g < m; g++) {
printf("%lld", xx[g]);
for (h = 0; h < k; h++)
printf(" %d", iii[g * k + h] + 1);
printf("\n");
}
return 0;
}
Compilation message (stderr)
nicegift.c: In function 'main':
nicegift.c:41:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
nicegift.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%lld", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |