답안 #41161

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
41161 2018-02-13T07:31:16 Z aome Gift (IZhO18_nicegift) C++14
100 / 100
783 ms 168880 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1000005;

int n, k;
long long sum, mx, a[N];
vector< vector< pair<long long, int> > > blocks;
vector< vector<long long> > res;

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i], sum += a[i], mx = max(mx, a[i]);
	}
	if (sum % k || mx > sum / k) {
		cout << -1; return 0;
	}
	long long avg = sum / k;
	long long cur = 0;
	vector< pair<long long, int > > vec;
	for (int i = 1; i <= n; ++i) {
		cur += a[i];
		if (cur >= avg) {
			vec.push_back(make_pair(a[i] - (cur - avg), i));
			blocks.push_back(vec), vec.clear(), cur -= avg;
			if (cur) {
				vec.push_back(make_pair(cur, i));
			} 
		}
		else vec.push_back(make_pair(a[i], i));
	}
/*	for (int i = 0; i < k; ++i) {
		cout << "#" << i << '\n';
		for (auto j : blocks[i]) cout << j.first << ' ' << j.second << '\n';
	}*/
	while (sum) {
		long long mn = 1e18;
		for (int i = 0; i < k; ++i) {
			mn = min(mn, blocks[i].back().first);
		}
		sum -= mn * k;
		vector<long long> vec;
		vec.push_back(mn);
		for (int i = 0; i < k; ++i) {
			blocks[i][blocks[i].size() - 1].first -= mn;
			vec.push_back(blocks[i].back().second);
			if (!blocks[i].back().first) blocks[i].pop_back();
		}
		res.push_back(vec);
	}
	cout << res.size() << '\n';
	for (int i = 0; i < res.size(); ++i) {
		for (auto j : res[i]) cout << j << ' '; cout << '\n';
	}
}

Compilation message

nicegift.cpp: In function 'int main()':
nicegift.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < res.size(); ++i) {
                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 376 KB n=4
2 Correct 1 ms 376 KB n=3
3 Correct 1 ms 440 KB n=3
4 Correct 1 ms 492 KB n=4
5 Correct 1 ms 512 KB n=4
6 Correct 1 ms 564 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 376 KB n=4
2 Correct 1 ms 376 KB n=3
3 Correct 1 ms 440 KB n=3
4 Correct 1 ms 492 KB n=4
5 Correct 1 ms 512 KB n=4
6 Correct 1 ms 564 KB n=2
7 Correct 1 ms 572 KB n=5
8 Correct 1 ms 576 KB n=8
9 Correct 1 ms 616 KB n=14
10 Correct 1 ms 640 KB n=11
11 Correct 27 ms 5364 KB n=50000
12 Correct 25 ms 5364 KB n=50000
13 Correct 1 ms 5364 KB n=10
14 Correct 2 ms 5364 KB n=685
15 Correct 3 ms 5364 KB n=623
16 Correct 2 ms 5364 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 376 KB n=4
2 Correct 1 ms 376 KB n=3
3 Correct 1 ms 440 KB n=3
4 Correct 1 ms 492 KB n=4
5 Correct 1 ms 512 KB n=4
6 Correct 1 ms 564 KB n=2
7 Correct 1 ms 572 KB n=5
8 Correct 1 ms 576 KB n=8
9 Correct 1 ms 616 KB n=14
10 Correct 1 ms 640 KB n=11
11 Correct 27 ms 5364 KB n=50000
12 Correct 25 ms 5364 KB n=50000
13 Correct 1 ms 5364 KB n=10
14 Correct 2 ms 5364 KB n=685
15 Correct 3 ms 5364 KB n=623
16 Correct 2 ms 5364 KB n=973
17 Correct 2 ms 5364 KB n=989
18 Correct 2 ms 5364 KB n=563
19 Correct 3 ms 5364 KB n=592
20 Correct 3 ms 5364 KB n=938
21 Correct 2 ms 5364 KB n=747
22 Correct 3 ms 5364 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 454 ms 87360 KB n=1000000
2 Correct 266 ms 87360 KB n=666666
3 Correct 138 ms 87360 KB n=400000
4 Correct 356 ms 87360 KB n=285714
5 Correct 9 ms 87360 KB n=20000
6 Correct 308 ms 87360 KB n=181818
7 Correct 8 ms 87360 KB n=10000
8 Correct 35 ms 87360 KB n=6666
9 Correct 3 ms 87360 KB n=4000
10 Correct 185 ms 87360 KB n=2857
11 Correct 4 ms 87360 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 376 KB n=4
2 Correct 1 ms 376 KB n=3
3 Correct 1 ms 440 KB n=3
4 Correct 1 ms 492 KB n=4
5 Correct 1 ms 512 KB n=4
6 Correct 1 ms 564 KB n=2
7 Correct 1 ms 572 KB n=5
8 Correct 1 ms 576 KB n=8
9 Correct 1 ms 616 KB n=14
10 Correct 1 ms 640 KB n=11
11 Correct 27 ms 5364 KB n=50000
12 Correct 25 ms 5364 KB n=50000
13 Correct 1 ms 5364 KB n=10
14 Correct 2 ms 5364 KB n=685
15 Correct 3 ms 5364 KB n=623
16 Correct 2 ms 5364 KB n=973
17 Correct 2 ms 5364 KB n=989
18 Correct 2 ms 5364 KB n=563
19 Correct 3 ms 5364 KB n=592
20 Correct 3 ms 5364 KB n=938
21 Correct 2 ms 5364 KB n=747
22 Correct 3 ms 5364 KB n=991
23 Correct 454 ms 87360 KB n=1000000
24 Correct 266 ms 87360 KB n=666666
25 Correct 138 ms 87360 KB n=400000
26 Correct 356 ms 87360 KB n=285714
27 Correct 9 ms 87360 KB n=20000
28 Correct 308 ms 87360 KB n=181818
29 Correct 8 ms 87360 KB n=10000
30 Correct 35 ms 87360 KB n=6666
31 Correct 3 ms 87360 KB n=4000
32 Correct 185 ms 87360 KB n=2857
33 Correct 4 ms 87360 KB n=2000
34 Correct 18 ms 87360 KB n=23514
35 Correct 18 ms 87360 KB n=23514
36 Correct 2 ms 87360 KB n=940
37 Correct 2 ms 87360 KB n=2
38 Correct 101 ms 87360 KB n=100000
39 Correct 105 ms 87360 KB n=100000
40 Correct 2 ms 87360 KB n=10
41 Correct 2 ms 87360 KB n=100
42 Correct 7 ms 87360 KB n=1000
43 Correct 719 ms 154780 KB n=1000000
44 Correct 783 ms 168880 KB n=1000000
45 Correct 581 ms 168880 KB n=666666
46 Correct 432 ms 168880 KB n=400000
47 Correct 186 ms 168880 KB n=2336
48 Correct 398 ms 168880 KB n=285714
49 Correct 364 ms 168880 KB n=181818
50 Correct 229 ms 168880 KB n=40000
51 Correct 228 ms 168880 KB n=20000
52 Correct 196 ms 168880 KB n=10000
53 Correct 202 ms 168880 KB n=6666
54 Correct 181 ms 168880 KB n=4000
55 Correct 254 ms 168880 KB n=2857
56 Correct 237 ms 168880 KB n=2000