-
Notifications
You must be signed in to change notification settings - Fork 0
/
RudolfandtheBallGame.java
200 lines (170 loc) · 4.01 KB
/
RudolfandtheBallGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Problem {
public static void main(String[] args) {
Template t = new Template();
int test = t.readInt();
StringBuilder sb = new StringBuilder();
while (test-- > 0) {
int n = t.readInt();
int m = t.readInt();
int x = t.readInt();
Queue<Integer> q = new LinkedList<>();
TreeSet<Integer> set[] = new TreeSet[2];
q.add(x);
for (int i = 0; i < 2; i++)
set[i] = new TreeSet<>();
int index = 0;
set[index].add(x);
for (int i = 0; i < m; i++) {
int dis = t.readInt();
char c = t.read().charAt(0);
int size = set[index].size();
while (size-- > 0) {
int cur = set[index].first();
set[index].remove(cur);
int first = cur + dis;
int second = cur - dis;
if (first > n)
first -= n;
if (second <= 0)
second += n;
if (c == '?' || c == '0') {
set[index ^ 1].add(first);
}
if (c == '?' || c == '1') {
set[index ^ 1].add(second);
}
}
index ^= 1;
}
sb.append(set[index].size() + "\n");
for (int it : set[index])
sb.append(it + " ");
sb.append("\n");
}
System.out.println(sb);
}
static class Template {
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st;
public int last(int a[], int target) {
int ind = -1;
int l = 0, r = a.length - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] <= target) {
ind = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ind;
}
public boolean isSorted(int a[]) {
for (int i = 0; i < a.length; i++) {
if (i + 1 < a.length && a[i] > a[i + 1])
return false;
}
return true;
}
public boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
public long factorial(int n) {
long fac = 1;
for (int i = 1; i <= n; i++)
fac *= i;
return fac;
}
public ArrayList<Integer> factors(int n) {
ArrayList<Integer> l = new ArrayList<>();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
l.add(i);
if (n / i != i)
l.add(n / i);
}
}
return l;
}
public void build(int seg[], int ind, int l, int r, int a[]) {
if (l == r) {
seg[ind] = a[l];
return;
}
int mid = (l + r) / 2;
build(seg, (2 * ind) + 1, l, mid, a);
build(seg, (2 * ind) + 2, mid + 1, r, a);
seg[ind] = Math.min(seg[(2 * ind) + 1], seg[(2 * ind) + 2]);
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public void swap(int a[], int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public void swap(char a[], int i, int j) {
char t = a[i];
a[i] = a[j];
a[j] = t;
}
public void swap(ArrayList<Integer> l, int i, int j) {
int t = l.get(i);
l.set(i, l.get(j));
l.set(j, t);
}
public void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int it : arr)
list.add(it);
Collections.sort(list);
int z = 0;
for (int i = 0; i < arr.length; i++)
arr[z++] = list.get(i);
}
public void sort(long arr[]) {
ArrayList<Long> list = new ArrayList<>();
for (long it : arr)
list.add(it);
Collections.sort(list);
int z = 0;
for (int i = 0; i < arr.length; i++)
arr[z++] = list.get(i);
}
public String read() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int readInt() {
return Integer.parseInt(read());
}
public long readLong() {
return Long.parseLong(read());
}
public double readDouble() {
return Double.parseDouble(read());
}
}
}