Closer Reviews Count More
You maintain a map of user reviews. Each review has a location and a score. When you want to estimate the score at a new location, nearby reviews should influence the estimate more than far ones. Use the k closest known reviews and weight each by w = 1/d, where d is the straight-line (Euclidean, L2) distance to the query point. If any known review is exactly at the query location (d = 0), ignore everyone else and predict the simple average of the scores of those exact matches.

The first line of the input contains a single integer n representing the number of known reviews. The next n lines contain two floating-point numbers x y followed by a floating-point score s, describing one review’s position and score.
The next line contains a single integer k representing how many nearby reviews to use.
The next line contains a single integer q representing the number of locations to estimate.
The last q lines each contain two floating-point numbers x y for the location to estimate.
The program should print the predicted scores.
Input | Output |
|---|---|
3 | 3 |
4 | 3.5 |
5 | 3.5 |
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB