Book Club
InterviewTime limit1sMemory limit128 MB
Given each of N cows' answers to NQ questions, count how many cows match all P given question-answer pairs simultaneously.
- Level
Medium4 of 10
- Topics
- Hash map, Implementation, Array, Brute force
- Solved
- No attempts yet
Problem
Bessie is looking for cows to join her book club. The herd has () cows numbered , but she wants only the most discerning and social cows.
Like a dating service, she created a questionnaire and asked each cow to answer its () questions (named ). These are questions such as "How much do you enjoy reading science fiction?", and every answer is an integer from to .
Your job is to tabulate the results and answer a query such as: "How many cows answered to question , to question , and to question ?" A query has () parts; part gives a question number () and a required answer (). Output a single integer: the number of cows that answered to question for every from to . A cow is counted only when it satisfies all parts at once.
For instance, a herd of cows answering questions might respond like this:
Cow Question
ID 1 2 3 4 5
1 1 1 1 1 1
2 1 2 3 4 5
3 1 2 1 2 3
4 2 1 1 2 2
Each row is one cow's answers; column is that cow's answer to question .
Input
- Line : three space-separated integers , , and .
- Lines : line contains space-separated integers, cow 's answers .
- Lines : line contains two space-separated integers and .
Output
- Line : a single integer, the number of cows that satisfy all of Bessie's criteria.