#include <bits/stdc++.h>
using namespace std;
// A. Circle Passing
// Problem Name circlepassing
// Time Limit 2 seconds
// Memory Limit 1 gigabyte
// It is the first day of high school for Anouk; as a warm-up activity, her sports teacher is making the
// class play name-learning games. There are 2N students in the class. Most of them do not know
// each other, but there are M pairs of best friends who do everything together. Each student has at
// most one best friend.
// The teacher arranges all of the students in a circle, consecutively assigning each student a number
// from 0 to 2N − 1. More specifically, for each 0 ≤ i < 2N − 1, students i and i + 1 stand next to
// each other. Additionally, students 0 and 2N − 1 stand next to each other.
// Since the teacher wants everyone to meet new students, best friends have to stand as far away
// from each other as possible, i.e. opposite each other. That is, the students forming the ith pair of
// best friends are standing at positions k and k + N respectively, where 0 ≤ k < N.
// The teacher selects two students x and y and hands a ball to student x. The goal is to send the ball
// to student y, but each student may only pass the ball to another student whose name they already
// know. Of course, best friends know each other's names. While the rules were explained, each
// student got to know the names of the two students standing directly beside them. Other than that,
// no one knows any other names.
// The game is played Q times; the teacher chooses two students each time. Since the students are
// not paying attention, they do not learn any new names throughout the games. What is the
// minimum number of passes needed to get the ball from student x to student y in each game?
// Input
// The first line of input contains three integers, N, M and Q, where 2N is the number of students
// in Anouk's class, M is the number of pairs of best friends, and Q is the number of games that are
// played.
// i i i
// circlepassing (1 of 5)
// The second line contains M integers k ,..., k , with k describing the ith pair of best friends.
// For each i, the best friends stand at positions k and k + N respectively. Each student has at most
// one best friend.
// The following Q lines each contain two integers, x and y , the two selected students in game i.
// Output
// Output Q lines, the ith line containing a single integer, the minimum number of passes needed in
// game i.
// Constraints and Scoring
// 2 ≤ N ≤ 5 ⋅ 10 .
// 1 ≤ M ≤ 5 ⋅ 10 and M ≤ N.
// 1 ≤ Q ≤ 2 ⋅ 10 .
// 0 ≤ k < k < ... < k < N.
// 0 ≤ x , y < 2N with x ≠ y .
// Your solution will be tested on a set of test groups, each worth a number of points. Each test
// group contains a set of test cases. To get the points for a test group, you need to solve all test
// cases in the test group.
// Group Score Limits
// 1 14
// M = 1 and x = k . In other words, there is a single pair of best friends, and
// in every game, the student starting with the ball has a best friend.
// 2 20 N,M,Q ≤ 1000
// 3 22 N ≤ 10 and M,Q ≤ 1000
// 4 17 x = 0 for all i
// 5 27 No additional constraints
// Examples
// The following two figures depict the arrangements in the first and the fourth sample. Two
// students are connected by an edge if they know each other's names.
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int N, M, Q;
cin >> N >> M >> Q;
set<int> bff;
for(int i = 0; i < M; i++){
int k;
cin >> k;
bff.insert(k);
bff.insert(k + N);
bff.insert(k - N);
bff.insert(k + 2 * N);
}
auto dist = [&](int a){
a %= (2 * N);
if(a < 0) a += (2 * N);
return min(a, (2 * N) - a);
};
for(int i = 0; i < Q; i++){
int x, y;
cin >> x >> y;
int ans = dist(y-x);
for(int a : {*prev(bff.lower_bound(x)), *bff.lower_bound(x)}){
ans = min(ans, dist(x - a) + 1 + dist(y - (a + N)));
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
279 ms |
66228 KB |
Output is correct |
4 |
Correct |
57 ms |
15492 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
8 ms |
1752 KB |
Output is correct |
15 |
Correct |
159 ms |
39680 KB |
Output is correct |
16 |
Correct |
159 ms |
38232 KB |
Output is correct |
17 |
Correct |
397 ms |
94552 KB |
Output is correct |
18 |
Correct |
394 ms |
94548 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
600 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
279 ms |
66228 KB |
Output is correct |
68 |
Correct |
57 ms |
15492 KB |
Output is correct |
69 |
Correct |
3 ms |
600 KB |
Output is correct |
70 |
Correct |
3 ms |
604 KB |
Output is correct |
71 |
Correct |
3 ms |
604 KB |
Output is correct |
72 |
Correct |
3 ms |
604 KB |
Output is correct |
73 |
Correct |
3 ms |
604 KB |
Output is correct |
74 |
Correct |
3 ms |
604 KB |
Output is correct |
75 |
Correct |
3 ms |
600 KB |
Output is correct |
76 |
Correct |
4 ms |
604 KB |
Output is correct |
77 |
Correct |
3 ms |
604 KB |
Output is correct |
78 |
Correct |
8 ms |
1752 KB |
Output is correct |
79 |
Correct |
159 ms |
39680 KB |
Output is correct |
80 |
Correct |
159 ms |
38232 KB |
Output is correct |
81 |
Correct |
397 ms |
94552 KB |
Output is correct |
82 |
Correct |
394 ms |
94548 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
3 ms |
344 KB |
Output is correct |
85 |
Correct |
2 ms |
344 KB |
Output is correct |
86 |
Correct |
2 ms |
348 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
3 ms |
348 KB |
Output is correct |
89 |
Correct |
3 ms |
604 KB |
Output is correct |
90 |
Correct |
3 ms |
604 KB |
Output is correct |
91 |
Correct |
3 ms |
604 KB |
Output is correct |
92 |
Correct |
5 ms |
604 KB |
Output is correct |
93 |
Correct |
4 ms |
604 KB |
Output is correct |
94 |
Correct |
1 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
0 ms |
348 KB |
Output is correct |
98 |
Correct |
0 ms |
348 KB |
Output is correct |
99 |
Correct |
0 ms |
348 KB |
Output is correct |
100 |
Correct |
0 ms |
348 KB |
Output is correct |
101 |
Correct |
1 ms |
604 KB |
Output is correct |
102 |
Correct |
0 ms |
348 KB |
Output is correct |
103 |
Correct |
0 ms |
348 KB |
Output is correct |
104 |
Correct |
0 ms |
348 KB |
Output is correct |
105 |
Correct |
0 ms |
348 KB |
Output is correct |
106 |
Correct |
0 ms |
348 KB |
Output is correct |
107 |
Correct |
0 ms |
348 KB |
Output is correct |
108 |
Correct |
0 ms |
344 KB |
Output is correct |
109 |
Correct |
0 ms |
348 KB |
Output is correct |
110 |
Correct |
0 ms |
348 KB |
Output is correct |
111 |
Correct |
0 ms |
348 KB |
Output is correct |
112 |
Correct |
1 ms |
604 KB |
Output is correct |
113 |
Correct |
0 ms |
348 KB |
Output is correct |
114 |
Correct |
0 ms |
348 KB |
Output is correct |
115 |
Correct |
0 ms |
348 KB |
Output is correct |
116 |
Correct |
0 ms |
344 KB |
Output is correct |
117 |
Correct |
1 ms |
604 KB |
Output is correct |
118 |
Correct |
0 ms |
348 KB |
Output is correct |
119 |
Correct |
0 ms |
348 KB |
Output is correct |
120 |
Correct |
1 ms |
348 KB |
Output is correct |
121 |
Correct |
272 ms |
66252 KB |
Output is correct |
122 |
Correct |
56 ms |
15700 KB |
Output is correct |
123 |
Correct |
5 ms |
604 KB |
Output is correct |
124 |
Correct |
3 ms |
604 KB |
Output is correct |
125 |
Correct |
4 ms |
404 KB |
Output is correct |
126 |
Correct |
3 ms |
604 KB |
Output is correct |
127 |
Correct |
3 ms |
472 KB |
Output is correct |
128 |
Correct |
3 ms |
604 KB |
Output is correct |
129 |
Correct |
3 ms |
612 KB |
Output is correct |
130 |
Correct |
4 ms |
600 KB |
Output is correct |
131 |
Correct |
4 ms |
604 KB |
Output is correct |
132 |
Correct |
8 ms |
1808 KB |
Output is correct |
133 |
Correct |
159 ms |
39508 KB |
Output is correct |
134 |
Correct |
146 ms |
37912 KB |
Output is correct |
135 |
Correct |
402 ms |
94404 KB |
Output is correct |
136 |
Correct |
417 ms |
94548 KB |
Output is correct |
137 |
Correct |
414 ms |
94544 KB |
Output is correct |
138 |
Correct |
437 ms |
94544 KB |
Output is correct |
139 |
Correct |
45 ms |
12880 KB |
Output is correct |
140 |
Correct |
14 ms |
4956 KB |
Output is correct |
141 |
Correct |
122 ms |
30656 KB |
Output is correct |
142 |
Correct |
185 ms |
43600 KB |
Output is correct |
143 |
Correct |
270 ms |
61524 KB |
Output is correct |
144 |
Correct |
4 ms |
600 KB |
Output is correct |
145 |
Correct |
3 ms |
604 KB |
Output is correct |
146 |
Correct |
3 ms |
604 KB |
Output is correct |
147 |
Correct |
4 ms |
604 KB |
Output is correct |
148 |
Correct |
9 ms |
604 KB |
Output is correct |
149 |
Correct |
4 ms |
604 KB |
Output is correct |
150 |
Correct |
9 ms |
1116 KB |
Output is correct |
151 |
Correct |
0 ms |
348 KB |
Output is correct |
152 |
Correct |
1 ms |
348 KB |
Output is correct |
153 |
Correct |
0 ms |
348 KB |
Output is correct |
154 |
Correct |
0 ms |
348 KB |
Output is correct |
155 |
Correct |
0 ms |
348 KB |
Output is correct |