Courier Constellation: Neighbors Decide the ETA

The Courier Constellation predicts delivery time in minutes from three clues: distance in kilometers, payload weight in kilograms, and the vehicle type: bike, car, or drone. Some vehicles fly, some weave through traffic, and some sit in it; to compare them fairly, times are estimated by looking at similar past jobs and averaging their outcomes.

You are asked to read a set of historical deliveries and then estimate the time for new requests using k-NN Regression. Make the two numeric features comparable by scaling them using the min-max scaling, with statistics computed from the historical data only. Represent the vehicle with a one-hot vector using the provided vocabulary and its order. Compare jobs with Euclidean distance using the combination of the two scaled numeric values together with the one-hot vehicle. Predict each time as the mean of its k nearest historical times. If a numeric column in the historical data is flat, its scaled value is 0. You may assume that all vehicles in the input appear in the given vocabulary.

The first line of the input contains a single integer v representing how many vehicle types there are. The second line contains v space-separated vehicle names: the vehicle vocabulary.

The next line contains two integers n k describing how many historical rows there are and the number of neighbors to use. Each of the next n lines contains four values - distance weight vehicle target_time.

The next line contains a single integer q representing how many new deliveries to estimate. Each of the next q lines contains distance weight vehicle for a new delivery.

The program should print q lines. Each line should contain a single real number - the predicted delivery time for that new request.

Input

Output

3
bike car drone
6 3
2 1 bike 12
5 3 bike 20
10 5 car 30
12 4 car 35
1 0.5 drone 8
3 2 bike 15
2
3 1 bike
11 5 car

15.666666666666666
28.333333333333332

3
bike car drone
9 3
1 0.4 drone 9
1.5 0.6 drone 10
0.8 0.3 drone 8
3 1.2 bike 15
4 1.8 bike 18
5 2.0 bike 20
10 4.5 car 32
12 5.0 car 36
14 6.0 car 40
3
1.2 0.5 drone
4.2 1.7 bike
13 5.5 car

9
17.666666666666668
36

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue