답안 #40511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
40511 2018-02-03T09:33:42 Z szawinis Nice sequence (IZhO18_sequence) C++14
100 / 100
698 ms 41540 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+1;

int n, m, dsu[N], nxt[N], pos[N], res[N];
bool mark[N];

int root(int v) { return (dsu[v] < 0 ? v : dsu[v] = root(dsu[v])); }
void merge(int u, int v) {
	nxt[u] = v;
	mark[v] = true;
	u = root(u), v = root(v);
	assert(u != v);
	dsu[u] += dsu[v];
	dsu[v] = u;
}

void solve() {
	scanf("%d %d", &n, &m);
	fill(dsu, dsu+N, -1);
	fill(nxt, nxt+N, -1);
	fill(pos, pos+N, -1);
	fill(mark, mark+N, 0);
	fill(res, res+N, 0);
	int k = 0;
	for(k = 1; k <= 2*max(n, m); k++) {
		// cout << k - n << ' ' << k << ' ' << endl;
		// cout << k << ' ' << k - m << endl;

		if(k - n >= 0 && k - m >= 0) {
			if(root(k - n) == root(k - m) || root(k) == root(k - n) || root(k) == root(k - m)) break;
			merge(k - n, k);
			merge(k, k - m);
		} else if(k - n >= 0) {
			if(root(k) == root(k - n)) break;
			merge(k - n, k);
		} else if(k - m >= 0) {
			if(root(k) == root(k - m)) break;
			merge(k, k - m);
		}
	}
	--k;
	vector<int> ord;
	for(int i = 0; i <= k; i++) if(!mark[i]) {
		ord.push_back(i);
		for(int j = ord.back(); nxt[j] != -1; j = nxt[j]) ord.push_back(nxt[j]);
		mark[i] = true;
	}

	// for(int i = 0; i <= k; i++) cout << nxt[i] << ' ';
	// cout << endl << endl;
	// assert(ord.size() == k+1);

	for(int i = 0; i <= k; i++) pos[ord[i]] = i;
	for(int i = 1; i <= k; i++) res[i] = pos[i-1] - pos[i];
	printf("%d\n", k);
	for(int i = 1; i <= k; i++) printf("%d ", res[i]);
	printf("\n");
}

int main() {
	int t;
	scanf("%d", &t);
	while(t--) solve();
}

Compilation message

sequence.cpp: In function 'void solve()':
sequence.cpp:19:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
                        ^
sequence.cpp: In function 'int main()':
sequence.cpp:63:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &t);
                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8568 KB Ok
2 Correct 20 ms 8672 KB Ok
3 Correct 20 ms 8724 KB Ok
4 Correct 20 ms 8796 KB Ok
5 Correct 21 ms 8796 KB Ok
6 Correct 20 ms 8796 KB Ok
7 Correct 20 ms 8796 KB Ok
8 Correct 30 ms 8868 KB Ok
9 Correct 30 ms 8920 KB Ok
10 Correct 21 ms 8920 KB Ok
11 Correct 21 ms 8972 KB Ok
12 Correct 20 ms 8972 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 8972 KB Ok
2 Correct 20 ms 8972 KB Ok
3 Correct 20 ms 8972 KB Ok
4 Correct 21 ms 8972 KB Ok
5 Correct 20 ms 9068 KB Ok
6 Correct 25 ms 9068 KB Ok
7 Correct 29 ms 9452 KB Ok
8 Correct 24 ms 9452 KB Ok
9 Correct 33 ms 9604 KB Ok
10 Correct 31 ms 9604 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9604 KB Ok
2 Correct 25 ms 9604 KB Ok
3 Correct 20 ms 9604 KB Ok
4 Correct 20 ms 9604 KB Ok
5 Correct 24 ms 9604 KB Ok
6 Correct 24 ms 9604 KB Ok
7 Correct 24 ms 9604 KB Ok
8 Correct 20 ms 9604 KB Ok
9 Correct 20 ms 9604 KB Ok
10 Correct 20 ms 9604 KB Ok
11 Correct 28 ms 9604 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 9604 KB Ok
2 Correct 22 ms 9604 KB Ok
3 Correct 22 ms 9604 KB Ok
4 Correct 20 ms 9604 KB Ok
5 Correct 20 ms 9604 KB Ok
6 Correct 111 ms 15252 KB Ok
7 Correct 93 ms 15252 KB Ok
8 Correct 204 ms 17892 KB Ok
9 Correct 141 ms 17892 KB Ok
10 Correct 123 ms 17892 KB Ok
11 Correct 125 ms 17892 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8568 KB Ok
2 Correct 20 ms 8672 KB Ok
3 Correct 20 ms 8724 KB Ok
4 Correct 20 ms 8796 KB Ok
5 Correct 21 ms 8796 KB Ok
6 Correct 20 ms 8796 KB Ok
7 Correct 20 ms 8796 KB Ok
8 Correct 30 ms 8868 KB Ok
9 Correct 30 ms 8920 KB Ok
10 Correct 21 ms 8920 KB Ok
11 Correct 21 ms 8972 KB Ok
12 Correct 20 ms 8972 KB Ok
13 Correct 13 ms 9604 KB Ok
14 Correct 25 ms 9604 KB Ok
15 Correct 20 ms 9604 KB Ok
16 Correct 20 ms 9604 KB Ok
17 Correct 24 ms 9604 KB Ok
18 Correct 24 ms 9604 KB Ok
19 Correct 24 ms 9604 KB Ok
20 Correct 20 ms 9604 KB Ok
21 Correct 20 ms 9604 KB Ok
22 Correct 20 ms 9604 KB Ok
23 Correct 28 ms 9604 KB Ok
24 Correct 22 ms 17892 KB Ok
25 Correct 21 ms 17892 KB Ok
26 Correct 22 ms 17892 KB Ok
27 Correct 35 ms 17892 KB Ok
28 Correct 25 ms 17892 KB Ok
29 Correct 23 ms 17892 KB Ok
30 Correct 21 ms 17892 KB Ok
31 Correct 22 ms 17892 KB Ok
32 Correct 21 ms 17892 KB Ok
33 Correct 23 ms 17892 KB Ok
34 Correct 25 ms 17892 KB Ok
35 Correct 23 ms 17892 KB Ok
36 Correct 23 ms 17892 KB Ok
37 Correct 23 ms 17892 KB Ok
38 Correct 25 ms 17892 KB Ok
39 Correct 24 ms 17892 KB Ok
40 Correct 26 ms 17892 KB Ok
41 Correct 23 ms 17892 KB Ok
42 Correct 26 ms 17892 KB Ok
43 Correct 25 ms 17892 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8568 KB Ok
2 Correct 20 ms 8672 KB Ok
3 Correct 20 ms 8724 KB Ok
4 Correct 20 ms 8796 KB Ok
5 Correct 21 ms 8796 KB Ok
6 Correct 20 ms 8796 KB Ok
7 Correct 20 ms 8796 KB Ok
8 Correct 30 ms 8868 KB Ok
9 Correct 30 ms 8920 KB Ok
10 Correct 21 ms 8920 KB Ok
11 Correct 21 ms 8972 KB Ok
12 Correct 20 ms 8972 KB Ok
13 Correct 22 ms 8972 KB Ok
14 Correct 20 ms 8972 KB Ok
15 Correct 20 ms 8972 KB Ok
16 Correct 21 ms 8972 KB Ok
17 Correct 20 ms 9068 KB Ok
18 Correct 25 ms 9068 KB Ok
19 Correct 29 ms 9452 KB Ok
20 Correct 24 ms 9452 KB Ok
21 Correct 33 ms 9604 KB Ok
22 Correct 31 ms 9604 KB Ok
23 Correct 13 ms 9604 KB Ok
24 Correct 25 ms 9604 KB Ok
25 Correct 20 ms 9604 KB Ok
26 Correct 20 ms 9604 KB Ok
27 Correct 24 ms 9604 KB Ok
28 Correct 24 ms 9604 KB Ok
29 Correct 24 ms 9604 KB Ok
30 Correct 20 ms 9604 KB Ok
31 Correct 20 ms 9604 KB Ok
32 Correct 20 ms 9604 KB Ok
33 Correct 28 ms 9604 KB Ok
34 Correct 22 ms 17892 KB Ok
35 Correct 21 ms 17892 KB Ok
36 Correct 22 ms 17892 KB Ok
37 Correct 35 ms 17892 KB Ok
38 Correct 25 ms 17892 KB Ok
39 Correct 23 ms 17892 KB Ok
40 Correct 21 ms 17892 KB Ok
41 Correct 22 ms 17892 KB Ok
42 Correct 21 ms 17892 KB Ok
43 Correct 23 ms 17892 KB Ok
44 Correct 25 ms 17892 KB Ok
45 Correct 23 ms 17892 KB Ok
46 Correct 23 ms 17892 KB Ok
47 Correct 23 ms 17892 KB Ok
48 Correct 25 ms 17892 KB Ok
49 Correct 24 ms 17892 KB Ok
50 Correct 26 ms 17892 KB Ok
51 Correct 23 ms 17892 KB Ok
52 Correct 26 ms 17892 KB Ok
53 Correct 25 ms 17892 KB Ok
54 Correct 95 ms 17892 KB Ok
55 Correct 107 ms 17892 KB Ok
56 Correct 121 ms 17892 KB Ok
57 Correct 77 ms 17892 KB Ok
58 Correct 96 ms 17892 KB Ok
59 Correct 88 ms 17892 KB Ok
60 Correct 117 ms 17892 KB Ok
61 Correct 81 ms 17892 KB Ok
62 Correct 113 ms 17892 KB Ok
63 Correct 84 ms 17892 KB Ok
64 Correct 136 ms 17892 KB Ok
65 Correct 94 ms 17892 KB Ok
66 Correct 89 ms 17892 KB Ok
67 Correct 77 ms 17892 KB Ok
68 Correct 98 ms 17892 KB Ok
69 Correct 161 ms 17892 KB Ok
70 Correct 161 ms 17892 KB Ok
71 Correct 156 ms 17892 KB Ok
72 Correct 154 ms 17892 KB Ok
73 Correct 159 ms 17892 KB Ok
74 Correct 167 ms 17892 KB Ok
75 Correct 162 ms 17892 KB Ok
76 Correct 154 ms 17892 KB Ok
77 Correct 153 ms 17892 KB Ok
78 Correct 164 ms 17892 KB Ok
79 Correct 168 ms 17892 KB Ok
80 Correct 162 ms 17892 KB Ok
81 Correct 163 ms 17892 KB Ok
82 Correct 161 ms 17892 KB Ok
83 Correct 152 ms 17892 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 8568 KB Ok
2 Correct 20 ms 8672 KB Ok
3 Correct 20 ms 8724 KB Ok
4 Correct 20 ms 8796 KB Ok
5 Correct 21 ms 8796 KB Ok
6 Correct 20 ms 8796 KB Ok
7 Correct 20 ms 8796 KB Ok
8 Correct 30 ms 8868 KB Ok
9 Correct 30 ms 8920 KB Ok
10 Correct 21 ms 8920 KB Ok
11 Correct 21 ms 8972 KB Ok
12 Correct 20 ms 8972 KB Ok
13 Correct 22 ms 8972 KB Ok
14 Correct 20 ms 8972 KB Ok
15 Correct 20 ms 8972 KB Ok
16 Correct 21 ms 8972 KB Ok
17 Correct 20 ms 9068 KB Ok
18 Correct 25 ms 9068 KB Ok
19 Correct 29 ms 9452 KB Ok
20 Correct 24 ms 9452 KB Ok
21 Correct 33 ms 9604 KB Ok
22 Correct 31 ms 9604 KB Ok
23 Correct 13 ms 9604 KB Ok
24 Correct 25 ms 9604 KB Ok
25 Correct 20 ms 9604 KB Ok
26 Correct 20 ms 9604 KB Ok
27 Correct 24 ms 9604 KB Ok
28 Correct 24 ms 9604 KB Ok
29 Correct 24 ms 9604 KB Ok
30 Correct 20 ms 9604 KB Ok
31 Correct 20 ms 9604 KB Ok
32 Correct 20 ms 9604 KB Ok
33 Correct 28 ms 9604 KB Ok
34 Correct 20 ms 9604 KB Ok
35 Correct 22 ms 9604 KB Ok
36 Correct 22 ms 9604 KB Ok
37 Correct 20 ms 9604 KB Ok
38 Correct 20 ms 9604 KB Ok
39 Correct 111 ms 15252 KB Ok
40 Correct 93 ms 15252 KB Ok
41 Correct 204 ms 17892 KB Ok
42 Correct 141 ms 17892 KB Ok
43 Correct 123 ms 17892 KB Ok
44 Correct 125 ms 17892 KB Ok
45 Correct 22 ms 17892 KB Ok
46 Correct 21 ms 17892 KB Ok
47 Correct 22 ms 17892 KB Ok
48 Correct 35 ms 17892 KB Ok
49 Correct 25 ms 17892 KB Ok
50 Correct 23 ms 17892 KB Ok
51 Correct 21 ms 17892 KB Ok
52 Correct 22 ms 17892 KB Ok
53 Correct 21 ms 17892 KB Ok
54 Correct 23 ms 17892 KB Ok
55 Correct 25 ms 17892 KB Ok
56 Correct 23 ms 17892 KB Ok
57 Correct 23 ms 17892 KB Ok
58 Correct 23 ms 17892 KB Ok
59 Correct 25 ms 17892 KB Ok
60 Correct 24 ms 17892 KB Ok
61 Correct 26 ms 17892 KB Ok
62 Correct 23 ms 17892 KB Ok
63 Correct 26 ms 17892 KB Ok
64 Correct 25 ms 17892 KB Ok
65 Correct 95 ms 17892 KB Ok
66 Correct 107 ms 17892 KB Ok
67 Correct 121 ms 17892 KB Ok
68 Correct 77 ms 17892 KB Ok
69 Correct 96 ms 17892 KB Ok
70 Correct 88 ms 17892 KB Ok
71 Correct 117 ms 17892 KB Ok
72 Correct 81 ms 17892 KB Ok
73 Correct 113 ms 17892 KB Ok
74 Correct 84 ms 17892 KB Ok
75 Correct 136 ms 17892 KB Ok
76 Correct 94 ms 17892 KB Ok
77 Correct 89 ms 17892 KB Ok
78 Correct 77 ms 17892 KB Ok
79 Correct 98 ms 17892 KB Ok
80 Correct 161 ms 17892 KB Ok
81 Correct 161 ms 17892 KB Ok
82 Correct 156 ms 17892 KB Ok
83 Correct 154 ms 17892 KB Ok
84 Correct 159 ms 17892 KB Ok
85 Correct 167 ms 17892 KB Ok
86 Correct 162 ms 17892 KB Ok
87 Correct 154 ms 17892 KB Ok
88 Correct 153 ms 17892 KB Ok
89 Correct 164 ms 17892 KB Ok
90 Correct 168 ms 17892 KB Ok
91 Correct 162 ms 17892 KB Ok
92 Correct 163 ms 17892 KB Ok
93 Correct 161 ms 17892 KB Ok
94 Correct 152 ms 17892 KB Ok
95 Correct 215 ms 17892 KB Ok
96 Correct 291 ms 20532 KB Ok
97 Correct 289 ms 20532 KB Ok
98 Correct 209 ms 20532 KB Ok
99 Correct 242 ms 20532 KB Ok
100 Correct 253 ms 20532 KB Ok
101 Correct 244 ms 20532 KB Ok
102 Correct 240 ms 20532 KB Ok
103 Correct 233 ms 20532 KB Ok
104 Correct 276 ms 20532 KB Ok
105 Correct 267 ms 21072 KB Ok
106 Correct 224 ms 21072 KB Ok
107 Correct 265 ms 21072 KB Ok
108 Correct 299 ms 21072 KB Ok
109 Correct 250 ms 21072 KB Ok
110 Correct 596 ms 39556 KB Ok
111 Correct 698 ms 39860 KB Ok
112 Correct 623 ms 41476 KB Ok
113 Correct 599 ms 41476 KB Ok
114 Correct 636 ms 41540 KB Ok
115 Correct 578 ms 41540 KB Ok
116 Correct 599 ms 41540 KB Ok
117 Correct 579 ms 41540 KB Ok
118 Correct 606 ms 41540 KB Ok
119 Correct 573 ms 41540 KB Ok
120 Correct 620 ms 41540 KB Ok
121 Correct 619 ms 41540 KB Ok
122 Correct 592 ms 41540 KB Ok
123 Correct 645 ms 41540 KB Ok
124 Correct 628 ms 41540 KB Ok
125 Correct 452 ms 41540 KB Ok