Popcorn Picks: Buckets Beat Distance
A cinema wants to recommend a genre by age group. Straight-line distances between ages don’t make sense; groups do. So ages are placed into buckets defined by given thresholds, and each bucket votes for its favorite genre.
You are asked to read a list of strictly increasing thresholds, then a preexisting set of viewer ages with their chosen genres. Every age belongs to exactly one bucket using these ranges:(-∞, t1), [t1, t2), …, [tk, +∞)
.

For each new age, predict the majority genre among preexisting rows in the same bucket. If that bucket has no rows, fall back to the global majority genre from all preexisting rows. Break any ties by choosing the lexicographically smallest label. Labels contain no spaces.
The first line of the input contains a single integer k
representing how many thresholds there are. The second line contains k
strictly increasing integers t1 ... tk
.
The next line contains a single integer n
representing how many preexisting rows follow. Each of the next n
lines contains an integer age and a label.
The next line contains a single integer q
representing how many query ages follow. Each of the next q
lines contains one integer age.
The program should print q
lines. Each line should contain a single label - the predicted genre for the corresponding query age.
Input | Output |
---|---|
2 | animation |
3 | comedy |
2 | comedy |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB