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.
@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: http://www.codechef.com/viewsolution/2819215
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: http://www.codechef.com/viewsolution/2803224 . 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.