hi all,
My solution
fails on sub task 3 {test case 7 and 9 }
Can someone tell me what is the test case 7 and 9 of subtask 3,
or can review my code.
Thanks in advance.
there will be a discussion tomorrow (by me) on Youtube Live Codechef channel as well as zoom. (I personally will be seeing the zoom chats so)
I also do the same and my code work, did you delete the path after traveling and check whether it already sorted or not?
https://www.codechef.com/viewsolution/32797596
Can anyone explain dp+bitmask part of the solution? Or provide some resources to understand it
Thanks in advance
I havenât deleted, why itâs needed? We can use subpath if required later.
No man you canât use subpart later as you are changing its position
I have commented what I understood from the Testerâs solution. Just posting here, so that it might be useful for someone. Also if there is anything wrong in my understanding, kindly please point out.
I have tried a polynomial N^2 algorithm and it worked. Here is my solution:
CodeChef: Practical coding for everyone.
But in the editorial, the proposed solution is exponential, so I wanted to know if my polynomial time solution is correct.
Basically, I tried a similar analogy from the M = 0 case. For each connected component, I tried to push the elements to its correct component in the best way possible based on similar intuition, which I couldnât prove to ensure minimum swaps. But it worked.
https://www.codechef.com/viewsolution/32990384
Can someone check my solution. I made graph and then started shorting all cycles.
Please can you elaborate your greedy approach ?
I am not able to wrap up the editorial around my head very clearly.
Thank you, it makes sense now.
It doesnât seem correct.
Try this case :
1
15 6
15 14 3 7 10 8 13 2 12 6 1 11 9 4 5
1 12
1 2
6 9
4 8
2 13
14 15
Based on setterâs code, it should give 10 as answer. Your code prints 11.
I think you just got lucky
The testerâs approach is not greedy. Its DP + Bitmask only.
I was able to solve this question in O(n+m) using DSU and DFS. Can anyone review my solution - CodeChef: Practical coding for everyone.
Maybe help me find some mistake in my approach
I was able to solve this using greedyâŚ
From each node i tried to find minimum size cycle and remove it and continue that thingâŚ
It feels to be right (but itâs greedy) can there be any counter case for my code
https://www.codechef.com/viewsolution/32751065
Thanks
You can try making a code that creates random testcases and compare your solution with setterâs solution. Thatâs how i found the testcase in the comments above
I also started enlarging the cycle by free swaps. Then I used dp with bitmask to split the cycles and check which of them gave me the minimum number of non-free swaps. It also got AC, but then I created some random testcases and compared my solution with the setterâs solution and it seems that this approach is wrong (or maybe I didnât implemented it well). It appears that the solution also depends on how you enlarge the cycle. If you just enlarge it with the first free swaps you find, then I think there could be created a counterexample.
If we notice, in whatever order we merge cycles (enlarging), the final cycle is always the same. and one more thing, if any value can be swapped to correct position with free swaps, I do it before enlarging, so the enlarged cycle doesnât have any free swaps between consecutive elements.
The split order can determine the outcome, so I am checking all the possible ways I can split the cycle using all the swaps with-in cycle. and then recursively calling it. Can you share your solution link, so I can go through it.
Thanks
Ashok
Hereâs the case for you:
1
18 7
14 15 16 13 2 17 5 10 8 7 6 12 4 18 1 11 9 3
1 4
9 16
1 11
18 5
12 6
3 16
5 14
This case was generated by a friend. This will fail for a lot of greedy approaches.
Correct answer is 11 .
I had the same approach earlier, removing cycles at each node greedily, but then I randomized it. So it mostly outputs correct now.
Can anyone let me know the probability of my submission failing?
I guess, you were lucky. Because your algorithm doesnât guarantee the identification of maximum number of cycles.
Consider the above graph of families all with equal weights 1. Your algorithm would wrongly choose cycle 012 first which leads to a total count of 2. But in reality the maximum number of cycles is 3.
Counter example -
1
9 3
5 4 6 7 9 8 2 3 1
1 7
2 8
3 9