Блог пользователя madver

Автор madver, история, 6 лет назад, По-английски

Hello everybody. Today, i hope you can help me to solve this problem. I have many ways to solve it if the array has distinct integers. But if the array has repeated elements, i think it is very hard. Can anyone give me some advice ?

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Haven't put much thought on this, but my guess would be n - k, where n is the size of the array and k is the value of longest not decreasing subsequence.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    for test

    2 3 4 ... n 1

    you would return 1 instead of n — 1

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Sure! Got it wrong. I think the problem it solves is when you are able to change the position of a element instead of swapping two. Any counter examples for this problem?

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

If you have several equal elements you will never make a swap between them, so you can replace each element with the tuple (ai, i). Notice that now all elements are distinct (and solving this problem is equivalent to solve the previous) and you can do whatever algo you know to solve this.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Would it work for the sequence 1 3 2 2?

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Of course. You need to sort the array: [(1, 1);(3, 2);(2, 3);(2, 4)]

      The sorted version will be: [(1, 1);(2, 3);(2, 4);(3, 2)]

      Notice when comparing tuple we first compare first element, and in case they differ we compare the second element.

      In this case you only need two swaps. Actually, after you replace each element by a tuple, you can replace each element by a different integer in the range [1, n], assigning each element its final position in the array, and now you need to solve the problem in a permutation. (Notice you need to sort the array first, to find it's final position).

      In this case (1,1) -> 1 (2,3) -> 2 (2,4) -> 3 (3,2) -> 4

      so the array you need to sort is:

      (1, 4, 2, 3)

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    Will this work if we allow to swap not only adjacent elements?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Friend of mine was asked the same question in Zalando interview round. Could not solve this too. Swapping any two elements is allowed. Anyone with a solution?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    I think you or your friend lied. Because there is currently a problem like this in USACO bronze

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

I have an idea based on the idea of finding "cycles" (kind of like in IOI 1996 "Sorting a Three-Valued Sequence"):

We sort the array, and make notes of where the original array and the sorted array are different. Then, we go from left to right, and whenever we find an index such that the two arrays differ, we do this process:

-in the original array, swap the element at the current index with the element that is at an index where it could be if the array were sorted (for example, if the max element is not where it should be, we would possibly swap it with the element located at the last index). Make sure that while making this swap the two elements have different values (so that it's not a useless swap).

-keep on continuing this process until an element that actually belongs at the current index is swapped in.