-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSleightOfHand.java
More file actions
255 lines (222 loc) · 7.24 KB
/
Copy pathSleightOfHand.java
File metadata and controls
255 lines (222 loc) · 7.24 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package algorithms.sprint1;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
// https://contest.yandex.ru/contest/22450/run-report/157017632/
public class SleightOfHand {
// -------------------- FAST INPUT --------------------
static final class FastIn {
private final InputStream in;
private final byte[] buf = new byte[1 << 16];
private int ptr = 0, len = 0;
FastIn(InputStream in) {
this.in = in;
}
private int read() throws IOException {
if (ptr >= len) {
len = in.read(buf);
ptr = 0;
if (len <= 0) return -1;
}
return buf[ptr++];
}
int nextInt() throws IOException {
int c;
do {
c = read();
if (c == -1) throw new EOFException("Unexpected EOF");
} while (c <= ' ');
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
int val = 0;
while (c > ' ') {
val = val * 10 + c - '0';
c = read();
}
return val * sign;
}
String next(int maxLength) throws IOException {
int c;
do {
c = read();
if (c == -1) throw new EOFException("Unexpected EOF");
} while (c <= ' ');
byte[] tmp = new byte[Math.min(32, maxLength)];
int n = 0;
while (c > ' ') {
if (n == maxLength) {
throw new IOException("Token length exceeds " + maxLength);
}
if (n == tmp.length) {
byte[] t2 = new byte[Math.min(maxLength, tmp.length * 2)];
System.arraycopy(tmp, 0, t2, 0, tmp.length);
tmp = t2;
}
tmp[n++] = (byte) c;
c = read();
if (c == -1) break;
}
return new String(tmp, 0, n, StandardCharsets.UTF_8);
}
}
// -------------------- FAST OUTPUT --------------------
static final class FastOut {
private final OutputStream out;
private final byte[] buf = new byte[1 << 16];
private int p = 0;
private final byte[] tmp = new byte[12];
FastOut(OutputStream out) {
this.out = out;
}
void writeByte(int b) throws IOException {
if (p == buf.length) flush();
buf[p++] = (byte) b;
}
void writeInt(int x) throws IOException {
if (x == 0) {
writeByte('0');
return;
}
if (x < 0) {
writeByte('-');
x = -x;
}
int k = 0;
while (x > 0) {
tmp[k++] = (byte) ('0' + (x % 10));
x /= 10;
}
for (int i = k - 1; i >= 0; i--) writeByte(tmp[i]);
}
void flush() throws IOException {
out.write(buf, 0, p);
p = 0;
}
}
public static void main(String[] args) throws Exception {
try {
if (System.getProperty("os.name").startsWith("Windows")) {
test();
} else {
run();
}
} catch (IOException | IllegalArgumentException e) {
System.err.println(e.getMessage());
}
}
private static void run() throws Exception {
FastIn in = new FastIn(System.in);
FastOut out = new FastOut(System.out);
int k = in.nextInt();
int limit = 2 * k;
int[] count = new int[10];
for (int r = 0; r < 4; r++) {
StringBuilder row = new StringBuilder(in.next(4));
// На всякий случай, если токенайзер разделит строку (обычно не будет)
while (row.length() < 4) {
row.append(in.next(4 - row.length()));
}
for (int c = 0; c < 4; c++) {
char ch = row.charAt(c);
if (ch != '.') {
if (ch < '0' || ch > '9') {
throw new IOException("Invalid grid cell: " + ch);
}
count[ch - '0']++;
}
}
}
int points = 0;
for (int d = 1; d <= 9; d++) {
int cnt = count[d];
if (cnt != 0 && cnt <= limit) {
points++;
}
}
out.writeInt(points);
out.writeByte('\n');
out.flush();
}
private static void test() {
// Примеры из условия
assertEq(2, solve(3, new int[][]{
{1, 2, 3, 1},
{2, 0, 0, 2},
{2, 0, 0, 2},
{2, 0, 0, 2}
}));
assertEq(1, solve(4, new int[][]{
{1, 1, 1, 1},
{9, 9, 9, 9},
{1, 1, 1, 1},
{9, 9, 1, 1}
}));
assertEq(0, solve(1, new int[][]{
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
}));
// Критичный тест: ровно 2*k нажатий — очко должно засчитаться
assertEq(1, solve(2, new int[][]{
{5, 5, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{5, 5, 0, 0}
}));
// Чуть больше лимита — очко не должно засчитаться
assertEq(0, solve(2, new int[][]{
{6, 6, 6, 0},
{6, 6, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
})); // 6 встречается 5 раз, limit=4
// Пустое поле
assertEq(0, solve(5, new int[][]{
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}));
// Несколько цифр в пределах лимита
assertEq(2, solve(1, new int[][]{
{1, 1, 2, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
})); // limit=2, cnt(1)=2, cnt(2)=1
System.out.println("Test OK");
}
static int solve(int k, int[][] a) {
int points = 0;
final int limit = 2 * k;
final int[] count = new int[10];
for (int[] row : a) {
for (int v : row) {
if (v != 0) {
if (v < 0 || v > 9) {
throw new IllegalArgumentException("Grid values must be between 0 and 9");
}
count[v]++;
}
}
}
for (int d = 1; d <= 9; d++) {
int cnt = count[d];
if (cnt != 0 && cnt <= limit) {
points++;
}
}
return points;
}
static void assertEq(int exp, int act) {
if (exp != act) {
throw new AssertionError("Expected=" + exp + ", actual=" + act);
}
}
}