Advertisement
Guest User

Jhaaaaaaa

a guest
Nov 9th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. // Jangan dicopas benar-benar semua ya gaes
  2. // Buat tuntunan aja
  3. // Kalo ada bagian yg bingung silakan pelajari sendiri
  4. // Semangat TP Nya
  5.  
  6. import java.io.*;
  7.  
  8. public class SDA18192T {
  9.     static int numBall, maxBall, event;
  10.  
  11.     public static void main(String[] args) {
  12.         readAndSolve();
  13.     }
  14.  
  15.     public static void readAndSolve() {
  16.         try {
  17.             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.             String data[] = reader.readLine().split(" ");
  19.             numBall = Integer.parseInt(data[0]);
  20.             maxBall = Integer.parseInt(data[1]);
  21.  
  22.             ZumaList zuma = new ZumaList();
  23.  
  24.             String initZuma[] = reader.readLine().split(" ");
  25.             for (int i = 0; i < numBall; i++) {
  26.                 zuma.add(Integer.parseInt(initZuma[i]));
  27.             }
  28.  
  29.             ZumaBall current = zuma.header.next;
  30.  
  31.             for (int i = 0; i < numBall; i++) {
  32.                 if (zuma.check(current)) {
  33.                     zuma.loss(current);
  34.                     break;
  35.                 }
  36.                 current = current.next;
  37.             }
  38.  
  39.             // zuma.printZuma();
  40.  
  41.             if (zuma.size < 4) {
  42.                 System.out.println("MENANG");
  43.                 return;
  44.             } else if (zuma.size > maxBall) {
  45.                 System.out.println("KALAH");
  46.                 return;
  47.             }
  48.  
  49.             event = Integer.parseInt(reader.readLine());
  50.  
  51.             for (int i = 0; i < event; i++) {
  52.                 String cmd[] = reader.readLine().split(" ");
  53.                 ZumaBall newBall = zuma.add(Integer.parseInt(cmd[0]), Integer.parseInt(cmd[1]));
  54.                 if (zuma.check(newBall)) {
  55.                     zuma.loss(newBall);
  56.                 }
  57.                 if (zuma.size < 4) {
  58.                     System.out.println("MENANG");
  59.                     return;
  60.                 } else if (zuma.size > maxBall) {
  61.                     System.out.println("KALAH");
  62.                     return;
  63.                 }
  64.                 // zuma.printZuma();
  65.             }
  66.             System.out.println(zuma.size);
  67.         } catch (Exception e) {
  68.             System.out.println(e);
  69.         }
  70.     }
  71. }
  72.  
  73. class ZumaBall {
  74.     int val;
  75.     ZumaBall next, prev;
  76.  
  77.     public ZumaBall(int val, ZumaBall prev, ZumaBall next) {
  78.         this.val = val;
  79.         this.prev = prev;
  80.         this.next = next;
  81.     }
  82. }
  83.  
  84. class ZumaList {
  85.     int size;
  86.     ZumaBall header;
  87.  
  88.     public ZumaList() {
  89.         header = new ZumaBall(0, null, null);
  90.         size = 0;
  91.     }
  92.  
  93.     public void add(int val) {
  94.         this.size++;
  95.         ZumaBall newNode;
  96.  
  97.         if (this.header.next == null) {
  98.             newNode = new ZumaBall(val, null, null);
  99.             newNode.next = newNode;
  100.             newNode.prev = newNode;
  101.             this.header.next = newNode;
  102.             return;
  103.         }
  104.  
  105.         newNode = new ZumaBall(val, this.header.next.prev, this.header.next);
  106.         newNode.prev.next = newNode;
  107.         newNode.next.prev = newNode;
  108.     }
  109.  
  110.     public ZumaBall add(int pos, int val) {
  111.         this.size++;
  112.         int position = pos;
  113.         ZumaBall current = this.header.next.prev;
  114.  
  115.         while (position > 0) {
  116.             current = current.next;
  117.             position--;
  118.         }
  119.  
  120.         ZumaBall newBall = new ZumaBall(val, current, current.next);
  121.         newBall.prev.next = newBall;
  122.         newBall.next.prev = newBall;
  123.  
  124.         if (pos == 0) {
  125.             this.header.next = newBall;
  126.         }
  127.  
  128.         return newBall;
  129.     }
  130.  
  131.     public boolean check(ZumaBall current) {
  132.         return (current.val == current.next.val && current.val == current.prev.val)
  133.                 || (current.val == current.next.val && current.val == current.next.next.val)
  134.                 || (current.val == current.prev.val && current.val == current.prev.prev.val);
  135.     }
  136.  
  137.     public void loss(ZumaBall current) {
  138.         ZumaBall now = current;
  139.         boolean headLoss = current.equals(this.header.next);
  140.         int sizeLoss = 1;
  141.  
  142.         while (now.val == now.prev.val) {
  143.             // headLoss = !headLoss ? now.equals(this.header.next) : true;
  144.             now = now.prev;
  145.             headLoss = !headLoss ? now.equals(this.header.next) : true;
  146.             sizeLoss++;
  147.         }
  148.  
  149.         ZumaBall left = now.prev;
  150.         now = current;
  151.  
  152.         while (now.val == now.next.val) {
  153.             // headLoss = !headLoss ? now.equals(this.header.next) : true;
  154.             now = now.next;
  155.             headLoss = !headLoss ? now.equals(this.header.next) : true;
  156.             sizeLoss++;
  157.         }
  158.  
  159.         ZumaBall right = now.next;
  160.  
  161.         this.size -= sizeLoss;
  162.         left.next = right;
  163.         right.prev = left;
  164.  
  165.         if (headLoss) {
  166.             this.header.next = right;
  167.         }
  168.  
  169.         if (this.size < 3)
  170.             return;
  171.  
  172.         if (this.check(right)) {
  173.             this.loss(right);
  174.         }
  175.     }
  176.  
  177.     public void printZuma() {
  178.         ZumaBall current = this.header.next;
  179.         String res = "";
  180.  
  181.         do {
  182.             res += current.val + " ";
  183.             current = current.next;
  184.         } while (!current.equals(this.header.next));
  185.  
  186.         System.out.println(res);
  187.         System.out.println("size: " + this.size);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement