Zhorzh

Untitled

Mar 10th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. let input = [
  2. '5 5',
  3. '1 2 1 2 5',
  4. ]
  5.  
  6. let print = this.print || console.log;
  7. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  8. const arr1 = gets().split(' ').map(Number);
  9. const arr2 = gets().split(' ').map(Number);
  10. const n = arr1[0];
  11. const k = arr1[1];
  12. class List {
  13. constructor() {
  14. this.items = []
  15. }
  16. add(item) {
  17. this.items.push(item);
  18. }
  19. get(index) {
  20. return this.items[index];
  21. }
  22. set(index, item) {
  23. return this.items[index] = item;
  24. }
  25. insert(index, item) {
  26. this.items.splice(index, 0, item);
  27. }
  28. remove(index) {
  29. this.items.splice(index, 1);
  30. }
  31. size() {
  32. return this.items.length;
  33. }
  34. values() {
  35. return [...this.items];
  36. }
  37. index(item) {
  38. return this.items.indexOf(item)
  39. }
  40. lastIndex(item) {
  41. return this.items.lastIndexOf(item)
  42. }
  43. }
  44.  
  45. let res = new List();
  46. for (let i = 1; i <= n; i++) {
  47. res.add(i);
  48. }
  49.  
  50. for (let i = 0; i < k; i++) {
  51. let currNum = arr2[i];
  52. let finalNum = 0;
  53. let ind = 0;
  54.  
  55. if (currNum % 2 === 0) {
  56. if (currNum !== n) {
  57. ind = res.index(n);
  58. res.remove(res.index(currNum));
  59. res.insert(ind, currNum);
  60. }
  61. finalNum = currNum / 2;
  62. ind = res.index(finalNum);
  63. res.remove(res.index(currNum))
  64. res.insert(ind+1, currNum);
  65. } else if (currNum % 2 === 1) {
  66.  
  67. if (currNum * 2 > n) {
  68. if (currNum !== n) {
  69. ind = res.index(n);
  70. res.remove(res.index(currNum));
  71. res.insert(ind, currNum);
  72. }
  73. } else {
  74. finalNum = currNum * 2;
  75. ind = res.index(finalNum);
  76. res.remove(res.index(currNum));
  77. res.insert(ind, currNum);
  78. }
  79. }
  80. }
  81.  
  82.  
  83. print(res.items.join(' '));
Advertisement
Add Comment
Please, Sign In to add comment