Choosing Pizza Toppings
Time limit1sMemory limit256 MB
Simulate the given repair loop that flips one unfulfilled topping of the first unhappy friend until every friend has more than a third of wishes met.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Sorting
- Solved
- No attempts yet
Problem
My friends and I are ordering one big pizza to share. Choosing what goes on it is not simple, because everyone wants something different. Gunnar wants bananas, Emma wants no bananas but does want olives, Marc insists on tomatoes, and so it goes.
Once we managed to find a set of toppings that fulfilled at least 2/3 of the wishes of every one of us, and we agreed that this was good enough. Then Lukas lost the note with that list on the way to the pizzeria, so we have to choose again. This time we lower the bar and look for a set of toppings that fulfils strictly more than 1/3 of the wishes of every friend.
Input
The first line contains an integer (), the number of friends in the group, myself included. Each of the next lines describes one friend. Such a line starts with an integer (), the number of wishes of that friend, followed by wishes separated by spaces. A wish is written as +topping or -topping. The wish +topping means the friend wants that topping on the pizza, and -topping means the friend wants it left off. Inside one line a topping name appears at most once.
A topping name is a string of 1 to 15 lowercase English letters. The input mentions at most 250 different toppings.
Output
The wish +topping is fulfilled when that topping is on the pizza, and the wish -topping is fulfilled when that topping is not on the pizza. A friend who has wishes and fulfilled wishes is happy when .
Print a set of toppings that makes every friend happy. You may assume that some set of toppings fulfils at least 2/3 of the wishes of every friend. Many sets can make everyone happy, so print exactly the set that the procedure below builds.
Number the friends 1 to in input order. Start with no topping on the pizza and with , then repeat the following.
- If every friend is happy, stop.
- Otherwise let be the smallest number of a friend who is not happy. List the wishes of friend that are not fulfilled, sorted by topping name in increasing order. Call this list and its length .
- Replace by , then set .
- Fulfil the wish at position of , counting positions from 0. Put its topping on the pizza if the wish is a
+wish, and take its topping off the pizza if the wish is a-wish.
When the procedure stops, print the toppings that are on the pizza in lexicographic order, one per line. Every printed topping appears in the input, and no topping is printed twice.