Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class SwappingsWithNode {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(br.readLine());
- String[] inp = br.readLine().split(" ");
- SwappingList list = new SwappingList(n);
- for (String anInp : inp) {
- int call = Integer.parseInt(anInp);
- list.call(call);
- }
- //list.print();
- list.printList();
- }
- private static class Node {
- private int value;
- private int from;
- private int to;
- private Node(int value) {
- this.value = value;
- this.from = 0 ;
- this.to = 0;
- }
- public int getValue() {
- return value;
- }
- public int getFrom() {
- return from;
- }
- public void setFrom(int from) {
- this.from = from;
- }
- public int getTo() {
- return to;
- }
- public void setTo(int to) {
- this.to = to;
- }
- @Override
- public String toString() {
- return String.format(" Value: %d; To: %d; From: %d", value, to, from);
- }
- }
- private static class SwappingList {
- private Node[] list;
- private Node head;
- private Node end;
- private int size;
- private SwappingList(int size) {
- this.size = size + 2; //size + head + end
- list = new Node[this.size];
- head = new Node(0 );
- end = new Node(size + 1);
- list[0] = head;
- list[this.size - 1] = end;
- for (int i = 1; i <= size ; i++) {
- list[i] = new Node(i);
- list[i - 1].setTo(i);
- list[i].setFrom(i - 1);
- }
- list[size].setTo(size + 1);
- list[size + 1].setFrom(size);
- }
- private void call(int call) {
- if (list[call].getFrom() == 0) { //if call is the first element (not counting head)
- int callTo = list[call].getTo();
- int pointingEnd = end.getFrom();
- list[call].setTo(end.value);
- list[call].setFrom(pointingEnd);
- head.setTo(callTo);
- end.setFrom(call);
- list[pointingEnd].setTo(call);
- list[callTo].setFrom(head.value);
- }else if (list[call].getTo() == end.value) { //if call is last element (not counting end)
- int callFrom = list[call].getFrom();
- int fromHead = head.getTo();
- list[call].setFrom(head.value);
- list[call].setTo(fromHead);
- head.setTo(call);
- end.setFrom(callFrom);
- list[callFrom].setTo(end.value);
- list[fromHead].setFrom(call);
- }else { //all other cases
- int callTo = list[call].getTo();
- int callFrom = list[call].getFrom();
- int pointingEnd = end.getFrom();
- int fromHead = head.getTo();
- list[call].setFrom(pointingEnd);
- list[call].setTo(fromHead);
- list[pointingEnd].setTo(call);
- list[fromHead].setFrom(call);
- list[callFrom].setTo(end.value);
- list[callTo].setFrom(head.value);
- head.setTo(callTo);
- end.setFrom(callFrom);
- }
- }
- private void print() {
- for (Node elem: list) {
- System.out.println(elem);
- }
- System.out.println();
- }
- private void printList() {
- int value = head.getTo();
- while (value != size - 1) {
- System.out.print(value + " ");
- value = list[value].getTo();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement