1 / 32
Apr 2007

Let i denote the goods that the i-th dealer wants. So our goal is to get i in the i-th position.

For the sample input....

Initial state: 2 1 3 5 6 7 4
On day one, the exchanges are 1-2, 4-5, and 7-6.
State after day 1: 1 2 3 4 7 6 5
On day two, the exchange is 7-5.
State after day 2: 1 2 3 4 5 6 7

However the output specification states that the pairs are the "dealers who exchange their goods," not the goods which are exchanged. This is what confused me.

Can someone clarify this for me? Before looking for flaws in my algorithm or implementation, I'd like to make sure I correctly understood what I'm supposed to output.

BUMP

Can anyone tell me a sample input for which a greedy approach wouldn't work? By greedy I mean find the first dealer who is not happy and make him/her happy, repeating this process until they all have the goods they want.

Ahhh...I'm pretty new to all this, so I'm not sure if this is what you're looking for, so...ignore me if I'm missing your question. =D

Consider a four-dealer layout: 3 1 4 2
If you had a greedy that strictly tried to make 1 happy, then 2, then 3, then 4, your program would function like this...
3 1 4 2
Make 1 happy...
1 3 4 2
Wait a day, then make 2 happy...
1 2 4 3
Wait a day, then make 3 and 4 happy...
1 2 3 4
So your program would claim that it would take three days (unless I'm misinterpreting what you mean, which is very possible).

But the real solution does this:
3 1 4 2
Make 1 and 4 happy on the first day (the transactions do not overlap).
1 3 2 4
Make 2 and 3 happy.
1 2 3 4
So the real solution takes only two days.

Still, not sure if greedy works even after taking that into account--I haven't attempted that problem yet.

for 3 1 42 a greedy method should make maximize swaps without over lap.

day1 1-3 and 4-2 => 1 3 2 4
day2 2-3 => 1 2 3 4.

with this logic I tried to check the answer generated byfor all permutations of 1 2 3 4 5 and seems to give correct answer, but the judge gives a WA.

Not sure how the judge checks for 1 of the many solutions that can exist.

Any test-case where this might fail ?

Quelloquialism, I should have explained my approach in more detail. Basically what I do is go through the list and look for the first number which isn't in the proper place. If I can swap it (i.e. if neither its source nor its destination has been in a transaction today), then I do so. Day ends when the end of the list is reached, and the process is repeated until all dealers are happy. So, for the example you gave, 3 1 4 2, my solution outputs:

2
2 3-4 2-1
1 4-1

For the sample input, my solution gives:

2
3 2-1 5-6 7-4
1 6-4

This is correct, isn't it?

That's my approach too. it also gave me WA. Still, I think the greedy is good.

my friend gave me this counter example.

6 1 2 3 4 5 ... the algorithm dicussed above gives the following 3 steps

3
3 1-6 3-2 5-4
1 2-6
1 4-6

but the correct answer should be

2
2 6-2 3-5
3 2-1 6-3 5-4

Interestingly the number of swaps remains same => the swaps needed to resolve the cycle of length N is still N-1; the only thing that changes is the order of the swaps.

Ah I see. I assumed that a greedy method would minimize number of days because it minimizes total transactions. However this was evidently not always the case.

Thanks to everyone for your comments, I've adjusted my solution and now I have AC. It's the slowest solution, though laughing
I guess that's what I get for using Java. (It's easier to blame it on the language than on my implementation. wink )

i also found the optimal solution, it gives the "optimal solution" on randomly generated N=5000 inputs and all inputs i try but when i submit it i always get WA
are there multiple test cases on one problem, or is there anything that i can miss or do wrong
my solution always does it with "optimal days"

I always find it funny how people say 'My answer is right, why am I getting WA?'. You are getting WA because your answer is wrong. So no, you haven't found the optimal solution, and your solution doesn't do it with optimal days. And of course we can't help with anything, since you haven't told us the wrong way you have tried to solve the problem.

Anybody can giveme some idea about this problem, general method or somethink.

Take the input:

10
3 2 1 7 4 5 6 9 10 8

This can be divided into several "cycles" i.e. smallest groups of numbers that can be rearranged among themselves to produce the correct permutation. The cycles are:

3 1 - length 2
2 - length 1
7 4 5 6 - length 4
9 10 8 - length 3

Obviously, cycles of length 1 are already solved and cycles of length 2 can be solved just by swapping the two numbers. But what would you do for the cycle of length 3 or 4? Or how about 6, like the example given by whiz? If all the numbers form a single cycle, then by the problem's constraints you might have to deal with a cycle as long as 5000.

Try solving by hand cycles of small lengths, and then you might think of a solution that works for any length.

Hello,

I tried a bit of a crazy idea on this problem, it seemed to work on every "small" test case that I built. The concept was to first identify cycles. Once you have them the way of ordering each cycle is as follows:
- Take the bigest and smallest value that haven't been swaped yet.
- Swap them.
- Reapet until the bigest and smallest values are the same.
After this, one day has passed, and the array is not necesarely ordered yet, so we reapet everything including the cycle identification until all cycles are of length 1.

Here is my output for problem's example:

input:
7
2 1 3 5 6 7 4

output:
3 2-1 7-4 6-5
1 6-4

It's not the same as the sample output but it is such as valid.

Here is my output for whiz's example:

input:
6
6 1 2 3 4 5

output:
3 6-1 5-2 4-3
2 6-2 5-3

But, I am getting a nice WA. Dos anyone know what could be wrong?

Thanks in advace.

TripleM is right. If the output is WA, you give the wrong answer. I won't tell the solution here, but just a hint: The number of days needed is bounded by a surprisingly low constant, not depending on cycle length, whereas the naive implementation, I also tried first, scales with O(log len). Any algorithm that is worse than O(1) days is correctly rejected as WA.

Examining the example with the 6-cycle that is solved in 2 instead of 3 days (given in this forum) pushed me to the right track. buldae_bien_'s algorithm happens to hit the optimal implementation under certain circumstances, for example in whiz's example, but does not do so in other cases, where a heuristic assumption included in it fails.

PS: I won't give any further hints to this problem. I go back squeezing more digits of SQRT(2).

Hello,

Thanks twice11, you were very right. I have found the constant that is an upper bound for this problem, regardless of the cycle length. What I don't seem to find is an algorithm that can fit to this constant for every test case. My latest non-implemented idea was to just swap with backtracking every posible swap combination until we only have length 2 cycles, but this would sure give TLE. I'm a bit frustraded with this problem. :S

I think with your 'lucky' previous idea you should be able to get to a correct algorithm.
Hint: you can relabel a cycle of length n to 'n 1 2 .. (n-1)'. The relabelling is easy enough, so all you need to do is work out how to solve that particular instance..

I am getting [color=red]runtime error (other)[/color] in this one.
Any idea of what does 'other' mean?

Y.

In most cases this means "memory limit exceeded".