Vassil_Iliev

sadasf

May 30th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const input =
  2.     [
  3.         '10 5',
  4.         '10 2 1 6 8'
  5.     ]
  6.  
  7. let print = this.print || console.log;
  8. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  9.  
  10.  
  11. class NodeList {
  12.     constructor(val) {
  13.         this.val = val;
  14.         this.next = null;
  15.         this.prev = null;
  16.     }
  17. }
  18.  
  19. class DLL {
  20.  
  21.     constructor() {
  22.         this.tail;
  23.         this.head;
  24.         this.map = new Map();
  25.     }
  26.  
  27.     createList(num) {
  28.         for (let i = 1; i <= num; i++) {
  29.  
  30.             if (!this.head) {
  31.                 const newNode = new NodeList(i);
  32.                 this.head = newNode
  33.                 this.tail = newNode;
  34.                 this.map.set(i, newNode);
  35.             } else {
  36.                 const newNode = new NodeList(i);
  37.                 newNode.prev = this.tail;
  38.                 this.tail.next = newNode;
  39.                 this.tail = newNode;
  40.                 this.map.set(i, newNode);
  41.             }
  42.         }
  43.     }
  44.  
  45.  
  46.  
  47.     iterate() {
  48.         let temp = this.head
  49.         let arr = [];
  50.         while (temp) {
  51.             arr.push(temp.val)
  52.             temp = temp.next;
  53.         }
  54.         return arr;
  55.     }
  56.  
  57.     shuffle(numberToShuffle) {
  58.         let insertAfter;
  59.  
  60.         if (numberToShuffle % 2 === 0) {
  61.             insertAfter = numberToShuffle / 2;
  62.         } else if (numberToShuffle % 2 != 0) {
  63.             insertAfter = numberToShuffle * 2;
  64.             if (insertAfter > this.map.size) {
  65.                 insertAfter = this.map.size;
  66.             }
  67.         }
  68.  
  69.         const nodeToRemove = this.map.get(numberToShuffle);
  70.  
  71.         if (nodeToRemove === this.head) {
  72.             this.head = this.head.next;
  73.             this.head.prev = null;
  74.             // nodeToRemove.prev = null;
  75.         } else if (nodeToRemove === this.tail) {
  76.  
  77.             this.tail = this.tail.prev;
  78.             this.tail.next = null;
  79.  
  80.         } else {
  81.             nodeToRemove.prev.next = nodeToRemove.next;
  82.             nodeToRemove.next.prev = nodeToRemove.prev;
  83.             // nodeToRemove.next = null;
  84.             // nodeToRemove.prev = null;
  85.         }
  86.  
  87.         const newNode = new NodeList(numberToShuffle);
  88.         console.log(newNode);
  89.         const nodeToInsertAfter = this.map.get(insertAfter);
  90.  
  91.         if (nodeToInsertAfter === this.tail) {
  92.             this.tail.next = newNode
  93.             this.tail = newNode;
  94.             this.map.set(numberToShuffle, newNode);
  95.         } else {
  96.             newNode.prev = nodeToInsertAfter;
  97.             newNode.next = nodeToInsertAfter.next;
  98.             nodeToInsertAfter.next = newNode;
  99.             this.map.set(numberToShuffle, newNode);
  100.  
  101.         }
  102.  
  103.     }
  104. }
  105.  
  106. const N = gets().split(' ').map(Number)
  107. const M = gets().split(' ').map(Number)
  108.  
  109.  
  110. const list = new DLL();
  111.  
  112. list.createList(N[0]);
  113.  
  114. for (let i = 0; i < N[1]; i++) {
  115.     list.shuffle(M[i]);
  116. }
  117.  
  118. print(list.iterate().join(' '));
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment