Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- class Deque {
- public static int[] d;
- public static int head = 0;
- public static int tail = 0;
- private static int n;
- public static int count = 0;
- Deque(int n) {
- this.n = n;
- d = new int[n];
- }
- public static void pushFront(int data) {
- head = (head - 1 + n) % n;
- d[head] = data;
- count++;
- }
- public static void pushBack(int data) {
- d[tail] = data;
- tail = (tail + 1) % n;
- count++;
- }
- public static int popFront() {
- int ans = d[head];
- head = (head + 1) % n;
- count--;
- return ans;
- }
- public static int popBack() {
- tail = (tail - 1 + n) % n;
- count--;
- return d[tail];
- }
- }
- public class K {
- public static void main(String[] args) throws IOException {
- try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("kenobi.in")));
- PrintWriter pr = new PrintWriter(new FileOutputStream("kenobi.out"))) {
- int n = Integer.parseInt(br.readLine());
- Deque deque = new Deque(n);
- for (int i = 0; i < n; i++) {
- String[] buf = br.readLine().split(" ");
- switch (buf[0]) {
- case "add":
- deque.pushBack(Integer.parseInt(buf[1]));
- break;
- case "take":
- deque.popBack();
- break;
- case "mum!":
- for (int j = 0; j < deque.count / 2; j++) {
- deque.pushBack(deque.popFront());
- }
- break;
- }
- }
- pr.println(deque.count);
- for (int i = deque.head; i < deque.tail; i++) {
- pr.print(deque.d[i] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement