Zhorzh

Untitled

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