Submission #50861

# Submission time Handle Problem Language Result Execution time Memory
50861 2018-06-13T17:55:17 Z Nicksechko Gift (IZhO18_nicegift) C++14
100 / 100
1035 ms 95192 KB
#ifndef LOCAL
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/
const int MAXN = 1000100;

vector<vector<ll>> ans;
int n, k;
pair<ll, ll> a[MAXN];
ll sum;
ll mx;

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; ++i)
		scanf("%lld", &a[i].first), sum += a[i].first, mx = max(mx, a[i].first), a[i].second = i + 1;
	sort(a, a + n);
	if (sum % k != 0 || mx > sum / k) {
		cout << -1 << "\n";
		return 0;
	}
	int lb = 0;
	int rb = n - 1;
	int len = k;
	ll rq = sum / k;
	while (rb - lb >= 0) {
		mx = a[rb].first;
		while (mx < rq && rb - lb + 1 > len) {
			if (a[lb].first == 0) {
				++lb;
				continue;
			}
			ll x = min(rq - mx, a[lb].first);
			vector<ll> vv;
			vv.push_back(x);
			for (int i = lb; i < lb + len; ++i)
				vv.push_back(a[i].second), a[i].first -= x;
			for (int i = rb + 1; i < n; ++i)
				vv.push_back(a[i].second), a[i].first -= x;
			ans.push_back(vv);
			rq -= x;
		}
		if (rb - lb + 1 == len) {
			vector<ll> vv;
			vv.push_back(a[n - 1].first);
			for (int i = lb; i < n; ++i)
				vv.push_back(a[i].second), a[i].first -= vv[0];
			ans.push_back(vv);
			break;
		}
		--rb;
		--len;
	}
	printf("%d\n", (int)ans.size());
	for (int i = 0; i < ans.size(); ++i) {
		for (ll j: ans[i])
			printf("%lld ", j);
		printf("\n");
	}
	return 0;
}


Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < ans.size(); ++i) {
                  ~~^~~~~~~~~~~~
nicegift.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~
nicegift.cpp:32:74: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &a[i].first), sum += a[i].first, mx = max(mx, a[i].first), a[i].second = i + 1;
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB n=4
2 Correct 2 ms 356 KB n=3
3 Correct 2 ms 356 KB n=3
4 Correct 2 ms 356 KB n=4
5 Correct 2 ms 392 KB n=4
6 Correct 2 ms 392 KB n=2
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB n=4
2 Correct 2 ms 356 KB n=3
3 Correct 2 ms 356 KB n=3
4 Correct 2 ms 356 KB n=4
5 Correct 2 ms 392 KB n=4
6 Correct 2 ms 392 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 3 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 27 ms 3492 KB n=50000
12 Correct 26 ms 3492 KB n=50000
13 Correct 2 ms 3492 KB n=10
14 Correct 3 ms 3492 KB n=685
15 Correct 2 ms 3492 KB n=623
16 Correct 3 ms 3492 KB n=973
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB n=4
2 Correct 2 ms 356 KB n=3
3 Correct 2 ms 356 KB n=3
4 Correct 2 ms 356 KB n=4
5 Correct 2 ms 392 KB n=4
6 Correct 2 ms 392 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 3 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 27 ms 3492 KB n=50000
12 Correct 26 ms 3492 KB n=50000
13 Correct 2 ms 3492 KB n=10
14 Correct 3 ms 3492 KB n=685
15 Correct 2 ms 3492 KB n=623
16 Correct 3 ms 3492 KB n=973
17 Correct 3 ms 3492 KB n=989
18 Correct 4 ms 3492 KB n=563
19 Correct 4 ms 3492 KB n=592
20 Correct 5 ms 3492 KB n=938
21 Correct 4 ms 3492 KB n=747
22 Correct 4 ms 3492 KB n=991
# Verdict Execution time Memory Grader output
1 Correct 538 ms 58084 KB n=1000000
2 Correct 323 ms 58084 KB n=666666
3 Correct 199 ms 58084 KB n=400000
4 Correct 132 ms 58084 KB n=285714
5 Correct 9 ms 58084 KB n=20000
6 Correct 66 ms 58084 KB n=181818
7 Correct 8 ms 58084 KB n=10000
8 Correct 6 ms 58084 KB n=6666
9 Correct 3 ms 58084 KB n=4000
10 Correct 6 ms 58084 KB n=2857
11 Correct 3 ms 58084 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB n=4
2 Correct 2 ms 356 KB n=3
3 Correct 2 ms 356 KB n=3
4 Correct 2 ms 356 KB n=4
5 Correct 2 ms 392 KB n=4
6 Correct 2 ms 392 KB n=2
7 Correct 2 ms 596 KB n=5
8 Correct 3 ms 596 KB n=8
9 Correct 2 ms 596 KB n=14
10 Correct 2 ms 596 KB n=11
11 Correct 27 ms 3492 KB n=50000
12 Correct 26 ms 3492 KB n=50000
13 Correct 2 ms 3492 KB n=10
14 Correct 3 ms 3492 KB n=685
15 Correct 2 ms 3492 KB n=623
16 Correct 3 ms 3492 KB n=973
17 Correct 3 ms 3492 KB n=989
18 Correct 4 ms 3492 KB n=563
19 Correct 4 ms 3492 KB n=592
20 Correct 5 ms 3492 KB n=938
21 Correct 4 ms 3492 KB n=747
22 Correct 4 ms 3492 KB n=991
23 Correct 538 ms 58084 KB n=1000000
24 Correct 323 ms 58084 KB n=666666
25 Correct 199 ms 58084 KB n=400000
26 Correct 132 ms 58084 KB n=285714
27 Correct 9 ms 58084 KB n=20000
28 Correct 66 ms 58084 KB n=181818
29 Correct 8 ms 58084 KB n=10000
30 Correct 6 ms 58084 KB n=6666
31 Correct 3 ms 58084 KB n=4000
32 Correct 6 ms 58084 KB n=2857
33 Correct 3 ms 58084 KB n=2000
34 Correct 21 ms 58084 KB n=23514
35 Correct 28 ms 58084 KB n=23514
36 Correct 3 ms 58084 KB n=940
37 Correct 2 ms 58084 KB n=2
38 Correct 122 ms 58084 KB n=100000
39 Correct 124 ms 58084 KB n=100000
40 Correct 3 ms 58084 KB n=10
41 Correct 3 ms 58084 KB n=100
42 Correct 8 ms 58084 KB n=1000
43 Correct 753 ms 92344 KB n=1000000
44 Correct 1035 ms 95192 KB n=1000000
45 Correct 861 ms 95192 KB n=666666
46 Correct 555 ms 95192 KB n=400000
47 Correct 229 ms 95192 KB n=2336
48 Correct 457 ms 95192 KB n=285714
49 Correct 487 ms 95192 KB n=181818
50 Correct 363 ms 95192 KB n=40000
51 Correct 292 ms 95192 KB n=20000
52 Correct 329 ms 95192 KB n=10000
53 Correct 299 ms 95192 KB n=6666
54 Correct 248 ms 95192 KB n=4000
55 Correct 248 ms 95192 KB n=2857
56 Correct 120 ms 95192 KB n=2000