답안 #287947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287947 2020-09-01T07:10:12 Z Berted Gift (IZhO18_nicegift) C++14
100 / 100
789 ms 142200 KB
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <assert.h>
#define ll long long
#define vi vector<int>
#define piv pair<ll, vector<int>>
#define pii pair<ll, int>
#define fst first
#define snd second
using namespace std;

int N, K;
ll S = 0;
pii A[1000001];
list<piv> L;

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("A.in", "r", stdin);
	//freopen("A.out", "w", stdout);
	cin >> N >> K;
	for (int i = 0; i < N; i++) {cin >> A[i].fst; A[i].snd = i; S += A[i].fst;}
	
	if (S % K) {cout << "-1\n";}
	else
	{
		sort(A, A + N, greater<pii>()); S /= K;
		if (A[0].fst <= S)
		{
			L.push_back({S, vi()});
			auto it = L.begin();
			int iterated = 0;
			for (int i = 0; i < N; i++)
			{
				while (A[i].fst)
				{
					if (it -> fst > A[i].fst)
					{
						it -> fst -= A[i].fst;
						vector<int> cpy = it -> snd;
						cpy.push_back(A[i].snd + 1);
						L.insert(it, make_pair(A[i].fst, cpy));
						A[i].fst = 0;
					}
					else
					{
						A[i].fst -= it -> fst;
						it -> snd.push_back(A[i].snd + 1);
						it = next(it);
						if (it == L.end()) {it = L.begin();}
					}
					iterated++;
				}
			}
			cout << L.size() << "\n";
			for (const auto &x : L)
			{
				cout << x.fst;
				for (const auto &y : x.snd) {cout << " " << y;}
				cout << "\n";
			}
			//cout << "Iteration Calls : " << iterated << "\n";
			assert(iterated <= N * K);
		}
		else {cout << "-1\n";}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 0 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 0 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 1 ms 384 KB n=5
8 Correct 1 ms 384 KB n=8
9 Correct 0 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 29 ms 6648 KB n=50000
12 Correct 31 ms 6656 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 1 ms 512 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 0 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 1 ms 384 KB n=5
8 Correct 1 ms 384 KB n=8
9 Correct 0 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 29 ms 6648 KB n=50000
12 Correct 31 ms 6656 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 1 ms 512 KB n=973
17 Correct 1 ms 512 KB n=989
18 Correct 1 ms 384 KB n=563
19 Correct 2 ms 512 KB n=592
20 Correct 2 ms 512 KB n=938
21 Correct 2 ms 512 KB n=747
22 Correct 2 ms 512 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 497 ms 82780 KB n=1000000
2 Correct 306 ms 45176 KB n=666666
3 Correct 156 ms 27128 KB n=400000
4 Correct 370 ms 63480 KB n=285714
5 Correct 7 ms 1152 KB n=20000
6 Correct 318 ms 49656 KB n=181818
7 Correct 4 ms 768 KB n=10000
8 Correct 35 ms 4224 KB n=6666
9 Correct 2 ms 512 KB n=4000
10 Correct 199 ms 22124 KB n=2857
11 Correct 1 ms 384 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB n=4
2 Correct 0 ms 384 KB n=3
3 Correct 1 ms 384 KB n=3
4 Correct 1 ms 384 KB n=4
5 Correct 1 ms 384 KB n=4
6 Correct 0 ms 384 KB n=2
7 Correct 1 ms 384 KB n=5
8 Correct 1 ms 384 KB n=8
9 Correct 0 ms 384 KB n=14
10 Correct 1 ms 384 KB n=11
11 Correct 29 ms 6648 KB n=50000
12 Correct 31 ms 6656 KB n=50000
13 Correct 1 ms 384 KB n=10
14 Correct 1 ms 384 KB n=685
15 Correct 1 ms 384 KB n=623
16 Correct 1 ms 512 KB n=973
17 Correct 1 ms 512 KB n=989
18 Correct 1 ms 384 KB n=563
19 Correct 2 ms 512 KB n=592
20 Correct 2 ms 512 KB n=938
21 Correct 2 ms 512 KB n=747
22 Correct 2 ms 512 KB n=991
23 Correct 497 ms 82780 KB n=1000000
24 Correct 306 ms 45176 KB n=666666
25 Correct 156 ms 27128 KB n=400000
26 Correct 370 ms 63480 KB n=285714
27 Correct 7 ms 1152 KB n=20000
28 Correct 318 ms 49656 KB n=181818
29 Correct 4 ms 768 KB n=10000
30 Correct 35 ms 4224 KB n=6666
31 Correct 2 ms 512 KB n=4000
32 Correct 199 ms 22124 KB n=2857
33 Correct 1 ms 384 KB n=2000
34 Correct 16 ms 3456 KB n=23514
35 Correct 17 ms 3456 KB n=23514
36 Correct 1 ms 512 KB n=940
37 Correct 0 ms 384 KB n=2
38 Correct 101 ms 16760 KB n=100000
39 Correct 103 ms 16824 KB n=100000
40 Correct 1 ms 384 KB n=10
41 Correct 1 ms 384 KB n=100
42 Correct 6 ms 896 KB n=1000
43 Correct 644 ms 136268 KB n=1000000
44 Correct 789 ms 142200 KB n=1000000
45 Correct 626 ms 101148 KB n=666666
46 Correct 471 ms 76468 KB n=400000
47 Correct 205 ms 21880 KB n=2336
48 Correct 395 ms 63480 KB n=285714
49 Correct 330 ms 49656 KB n=181818
50 Correct 274 ms 28612 KB n=40000
51 Correct 235 ms 25464 KB n=20000
52 Correct 206 ms 23160 KB n=10000
53 Correct 216 ms 23308 KB n=6666
54 Correct 176 ms 19792 KB n=4000
55 Correct 204 ms 22136 KB n=2857
56 Correct 100 ms 11000 KB n=2000