Zhorzh

Untitled

Mar 10th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. let input = [
  2. '7 4',
  3. '1 5 4 7',
  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. finalNum = currNum / 2;
  57. ind = res.index(finalNum);
  58. res.remove(res.index(currNum))
  59. res.insert(ind+1, currNum);
  60. } else if (currNum % 2 === 1) {
  61.  
  62. if (currNum * 2 > n) {
  63. if (currNum !== n) {
  64. ind = res.index(n);
  65. res.remove(res.index(currNum));
  66. res.insert(ind, currNum);
  67. }
  68. } else if (currNum * 2 < n) {
  69. finalNum = currNum * 2;
  70. ind = res.index(finalNum);
  71. res.remove(res.index(currNum));
  72. res.insert(ind, currNum);
  73. }
  74. }
  75. }
  76.  
  77.  
  78. print(res.items.join(' '));
  79.  
Advertisement
Add Comment
Please, Sign In to add comment