Could somebody tell me where does my solution go wrong?
Working for all the test cases in the editorial.
What I had done was to remove that column of demons which removes the maximum number of rows and vice versa
and if I’m not able to find such a row or column I remove that row or column which has the maximum number of demons.
I employed an approach similar to the first one and it failed for the test case given in the editorial. Have you tried that one? You can also try the test cases given by @kevinsogo in the comments under the editorial. Most probably you will find some fault in these cases.
Can anyone please tell if my greedy solution is failing for any test case. I am finding out the row or the column having minimum number of demon and then i am removing the corresponding row or columns of the minimum row or column. My solution is CodeChef: Practical coding for everyone.
@admin I feel that all those AC solutions which were done using greedy should be re-evaluated to WA as a greedy approach is bound to fail on some method or the other as shown by @kevinsogo on this thread and on many other Kamehameha threads.
best way to check is take a already made solution using “bi-partite matching” from solution and
compare result from greedy algorithm using randomly generated test cases. write a simple code for that
, if after millions tests u get same ans for both then try and mathematically prove the algorithm u designed to be optimal(or not optimal).
@kevinsogo Is every greedy approach bound to fail???
I got AC using following greedy approach-
1.Choose a row/column to attack which eliminates maximum number of columns/rows.
2.If no column or row is eliminated (by a row/column attack respectively),then find whether number of remaining columns are less or remaining rows and choose from it the row(if rows are less) or column(if columns are less) which has the maximum demons.
3.Repeat until all rows and columns are eliminated.
Code: CodeChef: Practical coding for everyone
I checked for all mentioned cases and it gives the right answer.
@kevinsogo the second approach fails.Your test cases are working for my code in the link.Lets say u find another test case on which my code doesn’t work.So does this mean that this can’t be solved greedily at all.
If you read the editorial, you’ll know that KMHAMHA can be reduced to an instance of the “maximum bipartite matching” problem. But you can also reduce an instance of the maximum bipartite matching problem into an instance of KMHAMHA!
In other words, if you solve this problem with a greedy approach, then you are also able to solve the maximum bipartite matching problem with a totally new approach. If you’re able to prove that it indeed works for all cases, then in my opinion this is a totally new algorithm that is worth publishing as a research paper.
@jaskaran_1 you should have mentioned that you changed the link in the question (e.g. “edited”) so I know that you updated it. Here’s your original link: CodeChef: Practical coding for everyone . I have a copy of your previous code and it fails for the large case.
Of course I can try to find a new test case where your new program fails, but see my comment above for a reason why greedy approaches are (almost) guaranteed to fail.