|
8 | 8 |
|
9 | 9 | public class FastSort { |
10 | 10 |
|
| 11 | + private static final int MAX_PARTICIPANTS = 100_000; |
| 12 | + private static final int MAX_LOGIN_LENGTH = 256; |
| 13 | + |
11 | 14 | /* |
12 | 15 | Принцип работы алгоритма: |
13 | 16 | 1) Считываем n участников в массив. |
@@ -140,51 +143,64 @@ private int read() throws IOException { |
140 | 143 | return buffer[ptr++]; |
141 | 144 | } |
142 | 145 |
|
143 | | - int nextInt() throws IOException { |
144 | | - int c; |
145 | | - do { |
146 | | - c = read(); |
147 | | - if (c == -1) { |
148 | | - throw new EOFException(); |
149 | | - } |
150 | | - } while (c <= ' '); |
151 | | - |
| 146 | + int nextInt(String fieldName, int minValue, int maxValue) throws IOException { |
| 147 | + int c = nextNonWhitespace(); |
152 | 148 | int value = 0; |
| 149 | + |
153 | 150 | while (c > ' ') { |
154 | | - value = value * 10 + c - '0'; |
| 151 | + if (c < '0' || c > '9') { |
| 152 | + throw new IOException("Invalid integer token for " + fieldName); |
| 153 | + } |
| 154 | + int digit = c - '0'; |
| 155 | + if (value > (maxValue - digit) / 10) { |
| 156 | + throw new IOException("Integer token for " + fieldName + " exceeds " + maxValue); |
| 157 | + } |
| 158 | + value = value * 10 + digit; |
155 | 159 | c = read(); |
156 | 160 | } |
| 161 | + |
| 162 | + if (value < minValue) { |
| 163 | + throw new IOException("Integer token for " + fieldName + " is below " + minValue); |
| 164 | + } |
157 | 165 | return value; |
158 | 166 | } |
159 | 167 |
|
160 | | - String next() throws IOException { |
| 168 | + String next(int maxLength) throws IOException { |
| 169 | + int c = nextNonWhitespace(); |
| 170 | + StringBuilder sb = new StringBuilder(Math.min(maxLength, 16)); |
| 171 | + |
| 172 | + while (c > ' ') { |
| 173 | + if (sb.length() == maxLength) { |
| 174 | + throw new IOException("Token length exceeds " + maxLength); |
| 175 | + } |
| 176 | + sb.append((char) c); |
| 177 | + c = read(); |
| 178 | + } |
| 179 | + return sb.toString(); |
| 180 | + } |
| 181 | + |
| 182 | + private int nextNonWhitespace() throws IOException { |
161 | 183 | int c; |
162 | 184 | do { |
163 | 185 | c = read(); |
164 | 186 | if (c == -1) { |
165 | 187 | throw new EOFException(); |
166 | 188 | } |
167 | 189 | } while (c <= ' '); |
168 | | - |
169 | | - StringBuilder sb = new StringBuilder(); |
170 | | - while (c > ' ') { |
171 | | - sb.append((char) c); |
172 | | - c = read(); |
173 | | - } |
174 | | - return sb.toString(); |
| 190 | + return c; |
175 | 191 | } |
176 | 192 | } |
177 | 193 |
|
178 | 194 | private static void run() throws Exception { |
179 | 195 | FastIn in = new FastIn(System.in); |
180 | 196 |
|
181 | | - int n = in.nextInt(); // n — количество участников |
| 197 | + int n = in.nextInt("participant count", 0, MAX_PARTICIPANTS); // n — количество участников |
182 | 198 | Participant[] participants = new Participant[n]; |
183 | 199 |
|
184 | 200 | for (int i = 0; i < n; i++) { |
185 | | - String login = in.next(); |
186 | | - int solved = in.nextInt(); |
187 | | - int penalty = in.nextInt(); |
| 201 | + String login = in.next(MAX_LOGIN_LENGTH); |
| 202 | + int solved = in.nextInt("solved", 0, Integer.MAX_VALUE); |
| 203 | + int penalty = in.nextInt("penalty", 0, Integer.MAX_VALUE); |
188 | 204 | participants[i] = new Participant(login, solved, penalty); |
189 | 205 | } |
190 | 206 |
|
|
0 commit comments