Practice Hash Code
Practice Hash Code
This is a practice problem which allows you to get familiar with the Hash Code Judge System
and problem format before the Online Quali cations. See g.co/hashcode/schedule for more
information.
Introduction
Isn't it fun to share pizza with friends? But, sometimes you just don't have enough time to choose
what pizza to order. Wouldn't it be nice if someone else chose for you?
In an imaginary world…
Pizza
Expecting many hungry customers, the pizzeria has already prepared some pizzas with di erent
ingredients. Each pizza can be delivered to at most one team. There can be multiple pizzas with
the exact same set of ingredients.
For example, there are 5 pizzas available in the pizzeria:
Pizza 0 Pizza 1 Pizza 2 Pizza 3 Pizza 4
Pizza 0: onion, pepper, olive
Pizza 1: mushroom, tomato, basil
Pizza 2: chicken, mushroom, pepper
Pizza 3: tomato, mushroom, basil
Pizza 4: chicken, basil
Note that Pizzas 1 and 3 have the same ingredients, even though they are mentioned in
di erent order.
Teams
Teams of 2, 3, or 4 people all ordered pizzas. Each team ordered one pizza per team member, but
did not specify what ingredients to put on the pizzas. The pizzeria might not deliver to a team (no
pizzas are sent to that team). However, if the order is delivered, exactly one pizza should be
available per person. For example, it is an error to send 3 pizzas to a 4-person team.
Goal
Given the description of the pizzas available, and the number of teams of 2, 3, or 4 people that
have ordered, decide which pizzas to send to each of the teams. The goal is to maximize, per
team, the number of di erent ingredients used in all their pizzas.
File format
The rst line of the input le contains the following integer numbers separated by single spaces:
● M ( 1 ≤ M ≤ 100 000 ) - the number of pizzas available in the pizzeria
● T2 ( 0 ≤ T 2 ≤ 50 000 ) - the number of 2-person teams
● T3 ( 0 ≤ T 3 ≤ 50 000 ) - the number of 3-person teams
● T4 ( 0 ≤ T 4 ≤ 50 000 ) - the number of 4-person teams
The next M lines describe the pizzas available. Each line contains (space separated):
● an integer I ( 1 ≤ I ≤ 10 000 ) - the number of ingredients,
● followed by the list of I ingredients - Each ingredient consists of lowercase ASCII le ers
and dash (-) characters, and its length can be between 1 and 20 characters in total. Each
ingredient in a pizza is di erent, but the same ingredient can appear on di erent pizzas.
Example
Input file Description
Example
Submission file Description
Validation
In order for the submission to be accepted:
● each pizza must be pa of at most one order,
● for all N-person teams, either nobody or everybody receives a pizza,
● there are TN or less deliveries to teams of N people.
Scoring
For each delivery, the delivery score is the square of the total number of di erent ingredients of
all the pizzas in the delivery. The total score is the sum of the scores for all deliveries.
For example, with the example input le and the example submission le above, there are
- 4 ingredients delivered to the two-person team (mushroom, tomato, basil, chicken). The
score for that team is 42 = 16
- 7 ingredients delivered to the tree-person team. The score for that team is 72 = 49.
- (The score is 0 for the two teams that didn't have their order delivered)
The total score is 16 + 49 = 65.