Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. class Deque {
  5.  
  6.     public static int[] d;
  7.     public static int head = 0;
  8.     public static int tail = 0;
  9.     private static int n;
  10.     public static int count = 0;
  11.  
  12.     Deque(int n) {
  13.         this.n = n;
  14.         d = new int[n];
  15.     }
  16.  
  17.     public static void pushFront(int data) {
  18.         head = (head - 1 + n) % n;
  19.         d[head] = data;
  20.         count++;
  21.     }
  22.  
  23.     public static void pushBack(int data) {
  24.         d[tail] = data;
  25.         tail = (tail + 1) % n;
  26.         count++;
  27.     }
  28.  
  29.     public static int popFront() {
  30.         int ans = d[head];
  31.         head = (head + 1) % n;
  32.         count--;
  33.         return ans;
  34.     }
  35.  
  36.     public static int popBack() {
  37.         tail = (tail - 1 + n) % n;
  38.         count--;
  39.         return d[tail];
  40.     }
  41.  
  42. }
  43.  
  44. public class K {
  45.  
  46.     public static void main(String[] args) throws IOException {
  47.         try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("kenobi.in")));
  48.              PrintWriter pr = new PrintWriter(new FileOutputStream("kenobi.out"))) {
  49.             int n = Integer.parseInt(br.readLine());
  50.             Deque deque = new Deque(n);
  51.             for (int i = 0; i < n; i++) {
  52.                 String[] buf = br.readLine().split(" ");
  53.                 switch (buf[0]) {
  54.                     case "add":
  55.                         deque.pushBack(Integer.parseInt(buf[1]));
  56.                         break;
  57.                     case "take":
  58.                         deque.popBack();
  59.                         break;
  60.                     case "mum!":
  61.                         for (int j = 0; j < deque.count / 2; j++) {
  62.                            deque.pushBack(deque.popFront());
  63.                         }
  64.                         break;
  65.                 }
  66.             }
  67.             pr.println(deque.count);
  68.             for (int i = deque.head; i < deque.tail; i++) {
  69.                 pr.print(deque.d[i] + " ");
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement