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.

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 description**–

*Due 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 ≤ 10*

^{5}) — 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·10*

^{5}) — the number of mouses in the price list.*The next m lines each describe another mouse. The i-th line contains first integer val*

_{i}(1 ≤ val_{i}≤ 10^{9}) — 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:

1 2 3 |
Dictionary<long, list<long="">> dictInfo01 = new Dictionary<long, list<long="">>(); dictInfo01.Add(1, new List()); dictInfo01.Add(2, new List()); |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
using System; using System.Collections.Generic; using System.Linq; class cf2 { static void Main() { string strInfo01 = Console.ReadLine(); long longCountA = long.Parse(strInfo01.Split()[0]); long longCountB = long.Parse(strInfo01.Split()[1]); long longCountAB = long.Parse(strInfo01.Split()[2]); long longTotalCount = long.Parse(Console.ReadLine()); long longTotalSum = 0; long longTotalBought = 0; Dictionary<long, List<long>> dictInfo01 = new Dictionary<long, List<long>>(); dictInfo01.Add(1, new List<long>()); dictInfo01.Add(2, new List<long>()); Dictionary<long, LinkedList<long>> dictInfo02 = new Dictionary<long, LinkedList<long>>(); dictInfo02.Add(1, new LinkedList<long>()); dictInfo02.Add(2, new LinkedList<long>()); for (long i = 1; i <= longTotalCount; i++) { string strInfo02 = Console.ReadLine(); string strInfo0201 = strInfo02.Split()[1].Substring(0, 1); long longValue = long.Parse(strInfo02.Split()[0]); if (strInfo0201 == "U") { dictInfo01[1].Add(longValue); } else { dictInfo01[2].Add(longValue); } } dictInfo01[1].Sort(); dictInfo01[2].Sort(); foreach (var item in dictInfo01[1]) { dictInfo02[1].AddLast(item); } foreach (var item in dictInfo01[2]) { dictInfo02[2].AddLast(item); } for (long i = 1; i <= longTotalCount; i++) { long longSmaller = 0; long longFromWhich = 0; if (dictInfo02[1].Count > 0) { longSmaller = dictInfo02[1].First(); longFromWhich = 1; } if (dictInfo02[2].Count > 0) { if (longFromWhich == 1) { if (dictInfo02[2].First() < longSmaller) { longSmaller = dictInfo02[2].First(); longFromWhich = 2; } } else { longSmaller = dictInfo02[2].First(); longFromWhich = 2; } } if (longFromWhich == 1) { dictInfo02[1].RemoveFirst(); if (longCountA > 0) { FixNumbers(ref longCountA, ref longTotalSum, ref longSmaller, ref longTotalBought); } else if (longCountAB > 0) { FixNumbers(ref longCountAB, ref longTotalSum, ref longSmaller, ref longTotalBought); } } else if (longFromWhich == 2) { dictInfo02[2].RemoveFirst(); if (longCountB > 0) { FixNumbers(ref longCountB, ref longTotalSum, ref longSmaller, ref longTotalBought); } else if (longCountAB > 0) { FixNumbers(ref longCountAB, ref longTotalSum, ref longSmaller, ref longTotalBought); } } } Console.WriteLine("{0} {1}", longTotalBought, longTotalSum); } private static void FixNumbers(ref long longCount,ref long LongTotalSum,ref long longSmaller,ref long longTotalBought) { longCount--; LongTotalSum += longSmaller; longTotalBought++; } } |