Advertisement
vladimirVenkov

Swapping

Jul 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class SwappingsWithNode {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.         int n = Integer.parseInt(br.readLine());
  9.         String[] inp = br.readLine().split(" ");
  10.         SwappingList list = new SwappingList(n);
  11.  
  12.         for (String anInp : inp) {
  13.             int call = Integer.parseInt(anInp);
  14.             list.call(call);
  15.         }
  16.         //list.print();
  17.         list.printList();
  18.  
  19.     }
  20.  
  21.     private static class Node {
  22.         private int value;
  23.         private int from;
  24.         private int to;
  25.  
  26.         private Node(int value) {
  27.             this.value = value;
  28.             this.from = 0 ;
  29.             this.to = 0;
  30.         }
  31.  
  32.         public int getValue() {
  33.             return value;
  34.         }
  35.  
  36.         public int getFrom() {
  37.             return from;
  38.         }
  39.  
  40.         public void setFrom(int from) {
  41.             this.from = from;
  42.         }
  43.  
  44.         public int getTo() {
  45.             return to;
  46.         }
  47.  
  48.         public void setTo(int to) {
  49.             this.to = to;
  50.         }
  51.  
  52.         @Override
  53.         public String toString() {
  54.             return String.format(" Value: %d; To: %d; From: %d", value, to, from);
  55.         }
  56.     }
  57.  
  58.     private static class SwappingList {
  59.         private Node[] list;
  60.         private Node head;
  61.         private Node end;
  62.         private int size;
  63.  
  64.         private SwappingList(int size) {
  65.             this.size = size + 2; //size + head + end
  66.             list = new Node[this.size];
  67.             head = new Node(0 );
  68.             end = new Node(size + 1);
  69.             list[0] = head;
  70.             list[this.size - 1] = end;
  71.  
  72.             for (int i = 1; i <= size ; i++) {
  73.                 list[i] = new Node(i);
  74.                 list[i - 1].setTo(i);
  75.                 list[i].setFrom(i - 1);
  76.             }
  77.             list[size].setTo(size + 1);
  78.             list[size + 1].setFrom(size);
  79.         }
  80.  
  81.         private void call(int call) {
  82.             if (list[call].getFrom() == 0) { //if call is the first element (not counting head)
  83.                 int callTo = list[call].getTo();
  84.                 int pointingEnd = end.getFrom();
  85.  
  86.                 list[call].setTo(end.value);
  87.                 list[call].setFrom(pointingEnd);
  88.                 head.setTo(callTo);
  89.                 end.setFrom(call);
  90.                 list[pointingEnd].setTo(call);
  91.                 list[callTo].setFrom(head.value);
  92.  
  93.             }else if (list[call].getTo() == end.value) { //if call is last element (not counting end)
  94.                 int callFrom = list[call].getFrom();
  95.                 int fromHead = head.getTo();
  96.  
  97.                 list[call].setFrom(head.value);
  98.                 list[call].setTo(fromHead);
  99.                 head.setTo(call);
  100.                 end.setFrom(callFrom);
  101.                 list[callFrom].setTo(end.value);
  102.                 list[fromHead].setFrom(call);
  103.             }else { //all other cases
  104.  
  105.                 int callTo = list[call].getTo();
  106.                 int callFrom = list[call].getFrom();
  107.                 int pointingEnd = end.getFrom();
  108.                 int fromHead = head.getTo();
  109.  
  110.                 list[call].setFrom(pointingEnd);
  111.                 list[call].setTo(fromHead);
  112.                 list[pointingEnd].setTo(call);
  113.                 list[fromHead].setFrom(call);
  114.                 list[callFrom].setTo(end.value);
  115.                 list[callTo].setFrom(head.value);
  116.                 head.setTo(callTo);
  117.                 end.setFrom(callFrom);
  118.             }
  119.  
  120.         }
  121.  
  122.         private void print() {
  123.             for (Node elem: list) {
  124.                 System.out.println(elem);
  125.             }
  126.             System.out.println();
  127.         }
  128.  
  129.         private void printList() {
  130.             int value = head.getTo();
  131.             while (value != size - 1) {
  132.                 System.out.print(value + " ");
  133.                 value = list[value].getTo();
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement