How to collect number with minimal number of rounds?

asked 2019-11-11 22:05:03 +0200

Jaakko Seppälä gravatar image

updated 2019-11-11 22:08:01 +0200

Has someone an idea how to solve the following problem?

Take the numbers 1,...,100000 and permute them in some way. At first you can make a swap of two numbers. Then you have to compute how many rounds it would take to collect numbers in ascending order. You have to collect numbers by every round by going left to right. In how many ways you can swap two numbers at the beginning to collect numbers in ascending order with minimum number of rounds?

For example, if numbers are from one to five and those at the beginning in order 3, 1, 5, 4, 2, then you can collect them in three rounds: On first round you collect 1, 2, on the second round 3, 4 and finally 5. But you can do one swap in three different ways to collect numbers in two rounds, namely

3, 4, 5, 1, 2
3, 1, 4, 5, 2
3, 1, 2, 4, 5

Five number sequence can be solved easily by brute force and I found an algorithm to collect 1000 numbers, but 100000 numbers needs maybe some kind of trick to compute fast how a specific swap at the beginning affects how many rounds it takes to collect numbers.

edit retag flag offensive close merge delete

Comments

Your algorithm for 1000 doesn't work for 10000? Anyway, you could look at this: https://www.geeksforgeeks.org/number-... (I have only glanced at it, can't vouch for its accuracy).

John Palmieri gravatar imageJohn Palmieri ( 2019-11-12 00:53:38 +0200 )edit

Yes. My efforts are in https://stackoverflow.com/questions/5... . It looks they takes too much memory or time.

Jaakko Seppälä gravatar imageJaakko Seppälä ( 2019-11-13 18:44:45 +0200 )edit