C# – Greedy Algorithm from CodeForces

One may write a few chapters in an algorithmic book for greedy algorithms. The funny part about them, is that there are a set of algorithmic problems, where the greedy solution tends to be the best one.

greed-1

What exactly is a greedy algorithm? Uncle google says that:

A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. The idea is that on every stage of solving our problem we tend to take the best decision without thinking about the “big picture” and doing this we achieve the optimum decision. It’s not something that happens a lot, but it happens, especially in algorithmic problems. Thus, CodeForces.com had an algorithmic competition some weeks ago. There, the second problem had a typical greedy solution.
Problem descriptionDue to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy.
Input – The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 105)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·105)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer vali (1 ≤ vali ≤ 109)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in.
Output – two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy.
What is the idea, behind the greedy algorithm? We simply read the input and we put it in a list, in a dictionary, depending on the type of mouse:

Then we sort the lists. And we use linkedLists, because of the RemoveFirst function, that those classes have. Then we start with the cheapest mouse and we take it if there is a computer that supports only it. If not, then we check if there is a PC supporting both types. If we find a suitable computer, we take the price of the mouse to the total prices and we remove the PC. Seems quite logical and greedy and it works.

Here comes the code:

Here is the code in GitHub.

Tagged with: , ,