Mobile Selection
Practice
5 (1 votes)
Algorithms
Binary search
Medium
Searching
Sorting
Hiring
Problem
32% Success 1963 Attempts 30 Points 2s Time Limit 512MB Memory 1024 KB Max Code

John has opened a new mobile shop. Each mobile is having following properties associated to it :

  • Operating System ( Windows, Android or iOS)
  • RAM Size (2, 4 or 8 GB)
  • Memory Space (32 or 64 GB)
  • Price
  • Rating (based on other features like camera, touch quality, etc)

Now a customer comes to the store and tells his choice of Operating System, RAM size, Memory Space and his budget that he is willing to spend on the mobile. John will give the customer that mobile phone whose rating is maximum which also satisfies the criteria of Operating System, RAM size and Memory Space as specified by that customer and also whose price lies within the customer’s budget. If more than one eligible phone have maximum rating then, he can give any phone to the customer.

Note : All the queries are independent of each other.


Hint: Maintain a list of phone prices and rating for each combination of OS,RAM and Memory. For a given combination, find the one with best rating in given budget.

Input Format

First line contains an integer N denoting number of mobile phones in John's shop.
Each of the next N lines contains a string S followed by four integers A, B, C and D respectively where S denotes the operating system of the phone, A denotes the ram size of the phone, B denotes the memory space of the phone, C denotes the price of the phone and D denotes the rating of the phone.
Next line contains an integer Q denoting the number of customers who have visited John's shop.
Each of the next Q lines contains a string H followed by three integers E, F and G respectively where H denotes the choice of operating system of the phone required by the customer, E denotes the choice of ram size of the phone required by the customer, F denotes the choice of memory space of the phone required by the customer and G denotes the budget of the customer.

Output Format

For each query print the rating of the mobile phone given to customer by John as specified by the rules mentioned above.
If there is no mobile phone available in John's shop following the customer's constraints, print 1.
Answer for each query should be in new line.

Input Constraints
\(1 \le N, Q \le 10^6\)
\(S, H\){ "windows", "android", "ios"}
\(A, E\){2, 4, 8}
\(B, F\){32, 64}
\(1 \le C, D, G \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
164 votes
Tags:
ApprovedBinary SearchMediumOpenSorting
Points:30
24 votes
Tags:
AlgorithmsApprovedData StructuresMediumOpenSorting
Points:50
14 votes
Tags:
Modular arithmeticHardApprovedModular exponentiationMathematicsNumber TheoryMatrix Exponentiation