Submission #768792

#TimeUsernameProblemLanguageResultExecution timeMemory
768792rainboyGift (IZhO18_nicegift)C11
100 / 100
495 ms64644 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...