HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Data Structures > problem:


50796 - KNN

Guest
• Review clarifications (1)

Volume problems

• 50709 - k-ary Strings
• 50708 - Binary Strings
• 50694 - The Cheapest Flight
• 50996 - Checkers - the shortest path
• 50770 - Average Depth
• 50291 - Postfix Arithmetic Expressions
• 50775 - Balanced Parenthesis
• 50736 - Top N Donors - 2
• 50796 - KNN
• 50340 - Game 19
• 50735 - Top M Products
• 50697 - Base Stations
• 50383 - Noisy Mornings
• 50479 - Bit Compressor
• 50674 - Collecting Eggs
• 50486 - Problems and Programmers
• 50695 - Longest link between neurons

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.
Prepared by Ibrahim Mesecan.

KNN

K Nearest Neighbour (KNN) is a famous classification method. It finds the K-nearest neighbors according to given metric distance. Then, it decides the class of current object according to majority of the nearest neighbors.

Assuming K=4, the right image shows the four close neighbors in a circle (3 red dots and 1 green plus). Since the majority of the neighbors are red dots, then the test sample is assigned to the class of red dots.

Question: Write a program that is going to classify m points according to dataset given. Assume that a point is closer if its Euclidean distance is smaller. If Euclidean distances are the same then, the node which is alphabetically smaller is closer.

Input specification
First, you will be given two integers (n and K) where n is between 0 and 5000 and K is between 1 and 100. Then, in the following n lines, you will be given the information for n objects where every line contains the following information:

  • x, y, z coordinates of the object where the coordinates are integers between -20000 and 20000
  • the class of the object: a string not more than 10 chars
Then, on the following line, you are given another integer (m): the number of querying objects. And, in the following m lines, you will be given 3D coordinates of m objects where m is between 1 and 100. There are not more than 100 classes.

Output specification
Show the classes of m objects in m lines. If there are many classes having the same number of majority votes, assign the class which is alphabetically greater. e.g. red is alphabetically greater than blue.

Sample Input I
9 4
1 3 0 red
1 2 0 red
2 2 0 red
2 5 0 red
1 5 0 red
3 4 0 blue
1 5 0 blue
2 5 0 blue
3 5 0 blue
1
3 3 0
Sample Output I
red



Для отправки решений необходимо выполнить вход.

www.contester.ru