|
| 1 | +import java.io.ByteArrayInputStream; |
| 2 | +import java.io.ByteArrayOutputStream; |
| 3 | +import java.io.EOFException; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.OutputStream; |
| 7 | + |
| 8 | +public class Deque { |
| 9 | + |
| 10 | + // -------------------- FAST INPUT -------------------- |
| 11 | + static final class FastIn { |
| 12 | + private final InputStream in; |
| 13 | + private final byte[] buf = new byte[1 << 16]; |
| 14 | + private int ptr = 0, len = 0; |
| 15 | + |
| 16 | + FastIn(InputStream in) { |
| 17 | + this.in = in; |
| 18 | + } |
| 19 | + |
| 20 | + private int read() throws IOException { |
| 21 | + if (ptr >= len) { |
| 22 | + len = in.read(buf); |
| 23 | + ptr = 0; |
| 24 | + if (len <= 0) { |
| 25 | + return -1; |
| 26 | + } |
| 27 | + } |
| 28 | + return buf[ptr++]; |
| 29 | + } |
| 30 | + |
| 31 | + int nextInt() throws IOException { |
| 32 | + int c; |
| 33 | + do { |
| 34 | + c = read(); |
| 35 | + if (c == -1) { |
| 36 | + throw new EOFException("Unexpected EOF"); |
| 37 | + } |
| 38 | + } while (c <= ' '); |
| 39 | + |
| 40 | + int sign = 1; |
| 41 | + if (c == '-') { |
| 42 | + sign = -1; |
| 43 | + c = read(); |
| 44 | + } |
| 45 | + |
| 46 | + int val = 0; |
| 47 | + while (c > ' ') { |
| 48 | + val = val * 10 + (c - '0'); |
| 49 | + c = read(); |
| 50 | + } |
| 51 | + return val * sign; |
| 52 | + } |
| 53 | + |
| 54 | + String next() throws IOException { |
| 55 | + int c; |
| 56 | + do { |
| 57 | + c = read(); |
| 58 | + if (c == -1) { |
| 59 | + throw new EOFException("Unexpected EOF"); |
| 60 | + } |
| 61 | + } while (c <= ' '); |
| 62 | + |
| 63 | + byte[] tmp = new byte[32]; |
| 64 | + int n = 0; |
| 65 | + while (c > ' ') { |
| 66 | + if (n == tmp.length) { |
| 67 | + byte[] t2 = new byte[tmp.length * 2]; |
| 68 | + System.arraycopy(tmp, 0, t2, 0, tmp.length); |
| 69 | + tmp = t2; |
| 70 | + } |
| 71 | + tmp[n++] = (byte) c; |
| 72 | + c = read(); |
| 73 | + if (c == -1) { |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + return new String(tmp, 0, n); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // -------------------- FAST OUTPUT -------------------- |
| 82 | + static final class FastOut { |
| 83 | + private final OutputStream out; |
| 84 | + private final byte[] buf = new byte[1 << 16]; |
| 85 | + private int p = 0; |
| 86 | + private final byte[] tmp = new byte[12]; |
| 87 | + |
| 88 | + FastOut(OutputStream out) { |
| 89 | + this.out = out; |
| 90 | + } |
| 91 | + |
| 92 | + void writeByte(int b) throws IOException { |
| 93 | + if (p == buf.length) { |
| 94 | + flush(); |
| 95 | + } |
| 96 | + buf[p++] = (byte) b; |
| 97 | + } |
| 98 | + |
| 99 | + void writeStr(String s) throws IOException { |
| 100 | + for (int i = 0; i < s.length(); i++) { |
| 101 | + writeByte(s.charAt(i)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void writeInt(int x) throws IOException { |
| 106 | + if (x == 0) { |
| 107 | + writeByte('0'); |
| 108 | + return; |
| 109 | + } |
| 110 | + if (x < 0) { |
| 111 | + writeByte('-'); |
| 112 | + x = -x; |
| 113 | + } |
| 114 | + |
| 115 | + int k = 0; |
| 116 | + while (x > 0) { |
| 117 | + tmp[k++] = (byte) ('0' + (x % 10)); |
| 118 | + x /= 10; |
| 119 | + } |
| 120 | + for (int i = k - 1; i >= 0; i--) { |
| 121 | + writeByte(tmp[i]); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void flush() throws IOException { |
| 126 | + out.write(buf, 0, p); |
| 127 | + p = 0; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // -------------------- RING BUFFER DEQUE -------------------- |
| 132 | + static final class RingDeque { |
| 133 | + private final int[] a; |
| 134 | + private final int cap; |
| 135 | + private int head = 0; // индекс первого элемента |
| 136 | + private int tail = 0; // индекс позиции "после последнего" |
| 137 | + private int size = 0; |
| 138 | + |
| 139 | + RingDeque(int cap) { |
| 140 | + this.cap = cap; |
| 141 | + this.a = new int[cap]; |
| 142 | + } |
| 143 | + |
| 144 | + private int next(int i) { |
| 145 | + i++; |
| 146 | + if (i == cap) { |
| 147 | + i = 0; |
| 148 | + } |
| 149 | + return i; |
| 150 | + } |
| 151 | + |
| 152 | + private int prev(int i) { |
| 153 | + i--; |
| 154 | + if (i == -1) { |
| 155 | + i = cap - 1; |
| 156 | + } |
| 157 | + return i; |
| 158 | + } |
| 159 | + |
| 160 | + boolean isEmpty() { |
| 161 | + return size == 0; |
| 162 | + } |
| 163 | + |
| 164 | + boolean isFull() { |
| 165 | + return size == cap; |
| 166 | + } |
| 167 | + |
| 168 | + void pushBack(int x) { |
| 169 | + a[tail] = x; |
| 170 | + tail = next(tail); |
| 171 | + size++; |
| 172 | + } |
| 173 | + |
| 174 | + void pushFront(int x) { |
| 175 | + head = prev(head); |
| 176 | + a[head] = x; |
| 177 | + size++; |
| 178 | + } |
| 179 | + |
| 180 | + int popFront() { |
| 181 | + int x = a[head]; |
| 182 | + head = next(head); |
| 183 | + size--; |
| 184 | + return x; |
| 185 | + } |
| 186 | + |
| 187 | + int popBack() { |
| 188 | + tail = prev(tail); |
| 189 | + int x = a[tail]; |
| 190 | + size--; |
| 191 | + return x; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private static void process(FastIn in, FastOut out) throws Exception { |
| 196 | + int n = in.nextInt(); |
| 197 | + int m = in.nextInt(); |
| 198 | + |
| 199 | + RingDeque dq = new RingDeque(m); |
| 200 | + |
| 201 | + for (int i = 0; i < n; i++) { |
| 202 | + String cmd = in.next(); |
| 203 | + |
| 204 | + if ("push_back".equals(cmd)) { |
| 205 | + int x = in.nextInt(); |
| 206 | + if (dq.isFull()) { |
| 207 | + out.writeStr("error\n"); |
| 208 | + } else { |
| 209 | + dq.pushBack(x); |
| 210 | + } |
| 211 | + } else if ("push_front".equals(cmd)) { |
| 212 | + int x = in.nextInt(); |
| 213 | + if (dq.isFull()) { |
| 214 | + out.writeStr("error\n"); |
| 215 | + } else { |
| 216 | + dq.pushFront(x); |
| 217 | + } |
| 218 | + } else if ("pop_front".equals(cmd)) { |
| 219 | + if (dq.isEmpty()) { |
| 220 | + out.writeStr("error\n"); |
| 221 | + } else { |
| 222 | + out.writeInt(dq.popFront()); |
| 223 | + out.writeByte('\n'); |
| 224 | + } |
| 225 | + } else { // pop_back |
| 226 | + if (dq.isEmpty()) { |
| 227 | + out.writeStr("error\n"); |
| 228 | + } else { |
| 229 | + out.writeInt(dq.popBack()); |
| 230 | + out.writeByte('\n'); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private static void run() throws Exception { |
| 237 | + FastIn in = new FastIn(System.in); |
| 238 | + FastOut out = new FastOut(System.out); |
| 239 | + process(in, out); |
| 240 | + out.flush(); |
| 241 | + } |
| 242 | + |
| 243 | + // -------------------- TESTS -------------------- |
| 244 | + private static String solveIO(String input) throws Exception { |
| 245 | + ByteArrayInputStream bin = new ByteArrayInputStream(input.getBytes()); |
| 246 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 247 | + FastIn in = new FastIn(bin); |
| 248 | + FastOut out = new FastOut(bout); |
| 249 | + process(in, out); |
| 250 | + out.flush(); |
| 251 | + return bout.toString(); |
| 252 | + } |
| 253 | + |
| 254 | + private static void test() throws Exception { |
| 255 | + // Пример 1 |
| 256 | + assertEq( |
| 257 | + "861\n-819\n", |
| 258 | + solveIO( |
| 259 | + "4\n" + |
| 260 | + "4\n" + |
| 261 | + "push_front 861\n" + |
| 262 | + "push_front -819\n" + |
| 263 | + "pop_back\n" + |
| 264 | + "pop_back\n" |
| 265 | + ) |
| 266 | + ); |
| 267 | + |
| 268 | + // Пример 2 |
| 269 | + assertEq( |
| 270 | + "-855\n0\n844\n", |
| 271 | + solveIO( |
| 272 | + "7\n" + |
| 273 | + "10\n" + |
| 274 | + "push_front -855\n" + |
| 275 | + "push_front 0\n" + |
| 276 | + "pop_back\n" + |
| 277 | + "pop_back\n" + |
| 278 | + "push_back 844\n" + |
| 279 | + "pop_back\n" + |
| 280 | + "push_back 823\n" |
| 281 | + ) |
| 282 | + ); |
| 283 | + |
| 284 | + // Пример 3 |
| 285 | + assertEq( |
| 286 | + "20\n102\n", |
| 287 | + solveIO( |
| 288 | + "6\n" + |
| 289 | + "6\n" + |
| 290 | + "push_front -201\n" + |
| 291 | + "push_back 959\n" + |
| 292 | + "push_back 102\n" + |
| 293 | + "push_front 20\n" + |
| 294 | + "pop_front\n" + |
| 295 | + "pop_back\n" |
| 296 | + ) |
| 297 | + ); |
| 298 | + |
| 299 | + // Емкость 1 + переполнение |
| 300 | + assertEq( |
| 301 | + "error\n1\nerror\n", |
| 302 | + solveIO( |
| 303 | + "4\n" + |
| 304 | + "1\n" + |
| 305 | + "push_front 1\n" + |
| 306 | + "push_back 2\n" + |
| 307 | + "pop_back\n" + |
| 308 | + "pop_front\n" |
| 309 | + ) |
| 310 | + ); |
| 311 | + |
| 312 | + // Проверка кольцевого перехода (wrap-around) |
| 313 | + assertEq( |
| 314 | + "1\n4\n2\n3\n", |
| 315 | + solveIO( |
| 316 | + "8\n" + |
| 317 | + "3\n" + |
| 318 | + "push_back 1\n" + |
| 319 | + "push_back 2\n" + |
| 320 | + "push_back 3\n" + |
| 321 | + "pop_front\n" + |
| 322 | + "push_back 4\n" + |
| 323 | + "pop_back\n" + |
| 324 | + "pop_front\n" + |
| 325 | + "pop_front\n" |
| 326 | + ) |
| 327 | + ); |
| 328 | + |
| 329 | + System.out.println("Test OK"); |
| 330 | + } |
| 331 | + |
| 332 | + static void assertEq(String exp, String act) { |
| 333 | + if (!exp.equals(act)) { |
| 334 | + throw new AssertionError("Expected:\n" + exp + "\nActual:\n" + act); |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + public static void main(String[] args) throws Exception { |
| 339 | + if (System.getProperty("os.name").startsWith("Windows")) { |
| 340 | + test(); |
| 341 | + } else { |
| 342 | + run(); |
| 343 | + } |
| 344 | + } |
| 345 | +} |
0 commit comments