Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Quora Nearby problem for CodeSprint2
- # Scored 9/10 test cases before Time Limit Reached disqualify the last test case... =/
- # Author: Kevin Tham
- def solve(topicsIndex, questions, queries):
- # Process the queries
- for query in queries:
- distances = []
- if query[0] == 't':
- # Calculate distance to all topics
- for topic in topicsIndex.values():
- distances.append((topic[0], specialDist(query[2], query[3], topic[1], topic[2])+tiebreak(topic[0])))
- elif query[0] == 'q':
- # Use dictionary to cache distance, as we move from question to question
- topicDists = {}
- # Find minimum distances for each question
- for question in questions:
- qTopics = []
- for tid in question[1]:
- # If we did not calculate the distance, do so now
- if not topicDists.has_key(tid) :
- # Cache distance into the dictionary
- topicDists[tid] = specialDist(query[2], query[3], topicsIndex[tid][1], topicsIndex[tid][2])
- qTopics.append((topicDists[tid],tid))
- if len(qTopics)>0:
- distances.append((question[0], min(qTopics)[0]+tiebreak(question[0])))
- distances.sort(key=lambda x:x[1])
- results = map(lambda x: x[0],distances[:query[1]])
- print'%s' % ' '.join(map(str, results))
- def tiebreak(idNum):
- # Using decimal to hold tiebreak value
- return 1.0/(idNum+1.0)
- def specialDist(x1,y1,x2,y2):
- return ((x2-x1)**2 + (y2-y1)**2)
- def main():
- T, Q, N = map(int,raw_input().split())
- topicsIndex = {}
- questions = []
- queries = []
- for t in xrange(T):
- args = raw_input().split()
- tid = int(args[0])
- topicsIndex[tid] = ((tid, float(args[1]),float(args[2])))
- for q in xrange(Q):
- args = raw_input().split()
- questions.append((int(args[0]), map(float, args[2:])))
- for q in xrange(N):
- args = raw_input().split()
- queries.append((args[0], int(args[1]), float(args[2]), float(args[3])))
- solve(topicsIndex, questions, queries)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement