roy-and-trending-topics-1

Roy is trying to develop a widget that shows Trending Topics (similar to Facebook) on the home page of HackerEarth Academy. 
He has gathered a list of N Topics (their IDs) and their popularity score (say z-score) from the database. Now z-score change everyday according to the following rules:
When a topic is mentioned in a 'Post', its z-score is increased by 50.  
A 'Like' on such a Post, increases the z-score by 5.  
A 'Comment' increases z-score by 10.  
A 'Share' causes an increment of 20.  

Now the Trending Topics are decided according to the change in z-score. One with the highest increment comes on top and list follows.
Roy seeks your help to write an algorithm to find the top 5 Trending Topics.
If change in z-score for any two topics is same, then rank them according to their ID (one with higher ID gets priority). It is guaranteed that IDs will be unique.  

Input format:
First line contains integer N
N lines follow
Each contains 6 space separated numbers representing Topic ID, current z-score - Z, Posts - P, Likes - L, Comments - C, Shares - S

Output format:
Print top 5 Topics each in a new line.
Each line should contain two space separated integers, Topic ID and new z-score of the topic.  

Constraints:
1 ≤ N ≤ 10^6 
1 ≤ ID ≤ 10^9   
0 ≤ Z, P, L, C, S ≤ 10^9  

Sample Test Explanation:
Now sort them according to the change in z-score. Change in z-score for IDs 999, 1001, 1002, 1003 is 100, so sort them according to their ID (one with the higher ID gets priority). Similarly proceed further.

After you get the top 5 list of Topic IDs according to the above criteria, find the new z-score from the table for each ID.

SAMPLE INPUT
8
1003 100 4 0 0 0
1002 200 6 0 0 0
1001 300 8 0 0 0
1004 100 3 0 0 0
1005 200 3 0 0 0
1006 300 5 0 0 0
1007 100 3 0 0 0
999 100 4 0 0 0SAMPLE OUTPUT
1003 200
1002 300
1001 400
999 200
1007 150

Source: hackerearth | Difficulty: EASY
Tags: Sorting

Data structures

Implementation


Solutions:
n = int(input())
topics = [[0 for x in range(3)] for x in range(n)]
for i in range(n):
	topics[i][0], topics[i][1], p, l, c, s = list(map(int, input().split()))
	topics[i][2] = (p*50 + l*5 + c*10 + s*20) - topics[i][1]
	topics[i][1] += topics[i][2]
topics.sort(key = lambda x : (x[2], x[0]))
topics.reverse()
for i in range(5):
	print(str(topics[i][0]) + " " + str(topics[i][1]))


n = int(input())
ans = []
for i in range(n):
	lst = list(map(int,input().split()))
	ans.append([lst[2]*50+lst[3]*5+lst[4]*10+lst[5]*20-lst[1],lst[0],lst[1]])
ans = sorted(ans,reverse = True)
if n<5:
	for a in ans:
		print(a[1],a[2]+a[0])
else:
	for i in range(5):
		print(ans[i][1],ans[i][2]+ans[i][0])


class Topics:
	def __init__(self,id1,changeScore,score):
		self.id1 = id1
		self.changeScore = changeScore
		self.newScore = score
	def getId(self):
		return self.id1
	def getScore(self):
		if self.newScore != None:
			return self.newScore
		else:
			return 
		return self.newScore
	def getChangeScore(self):
		return self.changeScore

class MinHeap:

	def __init__(self):
		self.items = [0]
		self.current_size = 0

	def insert(self,data):
		if (self.current_size == 0):
			self.current_size+=1
			self.items.append(data)
		else:
			self.items.append(data)
			self.current_size+=1
			self.percUp(self.current_size)

	def percUp(self,current_index):
		parent_index = current_index / 2
		while(parent_index >= 1 and self.items[parent_index].getChangeScore() <= self.items[current_index].getChangeScore()):
			if (self.items[parent_index].getChangeScore() == self.items[current_index].getChangeScore()):
				if (self.items[parent_index].getId() < self.items[current_index].getId()):
					temp = self.items[parent_index]
					self.items[parent_index] = self.items[current_index]
					self.items[current_index] = temp
			else:
				temp = self.items[parent_index]
				self.items[parent_index] = self.items[current_index]
				self.items[current_index] = temp
			current_index = parent_index
			parent_index = current_index / 2
			

	def printItems(self):
		for i in range(1,len(self.items)):
			print(self.items[i], end=' ')
		print()

	def size(self):
		return len(self.items)-1

	def isEmpty(self):
		return (self.current_size == 0)

	def delMin(self):
		min = self.items[1]
		self.items[1] = self.items[len(self.items)-1]
		self.items.pop()
		self.current_size-=1
		if not self.isEmpty():
			self.percDown(1)
		return min

	def percDown(self,parent_index):
		while (parent_index * 2) <= self.current_size:
			min_index = self.get_Min_Index(parent_index)
			if (self.items[parent_index].getChangeScore() <= self.items[min_index].getChangeScore()):
				if(self.items[parent_index].getChangeScore() == self.items[min_index].getChangeScore()):
					if (self.items[parent_index].getId() < self.items[min_index].getId()):
						temp = self.items[parent_index]
						self.items[parent_index] = self.items[min_index]
						self.items[min_index] = temp
				else:
					temp = self.items[parent_index]
					self.items[parent_index] = self.items[min_index]
					self.items[min_index] = temp
			parent_index = min_index


	def get_Min_Index(self,parent_index):
		lchild_index = 2 * parent_index
		rchlid_index = 2 * parent_index + 1
		if rchlid_index > self.current_size:
			return lchild_index
		else:
			if (self.items[lchild_index].getChangeScore() >= self.items[rchlid_index].getChangeScore()):
				if (self.items[lchild_index].getChangeScore() == self.items[rchlid_index].getChangeScore()):
					if (self.items[lchild_index].getId() > self.items[rchlid_index].getId()):
						return lchild_index
					else:
						return rchlid_index
				else:
					if (self.items[lchild_index].getChangeScore() > self.items[rchlid_index].getChangeScore()):
						return lchild_index
					else:
						return rchlid_index
			else:
				return rchlid_index
				# if (self.items[lchild_index].getChangeScore() < self.items[rchlid_index].getChangeScore()):
				# 	return lchild_index
				# else:
				# 	return rchlid_index

	def findMin(self):
		if not self.isEmpty():
			return self.items[1]
		else:
			return -1

heap = MinHeap()
for i in range(int(input())):
	id1 = 0
	oldScore = 0
	newScore = 0
	tps = [int(x) for x in input().strip().split(" ")]
	oldScore = tps[1]
	id1 = tps[0]
	if (tps[2] != 0):
		newScore+=(tps[2] * 50)
	if (tps[3] != 0):
		newScore+=(tps[3] * 5)
	if (tps[4] != 0):
		newScore+=(tps[4] * 10)
	if (tps[5] != 0):
		newScore+=(tps[5] * 20)
	changeScore = newScore - oldScore
	t = Topics(id1,changeScore,newScore)
	heap.insert(t)
count  = 5
while (count > 0):
	out = heap.delMin()
	print(out.getId(),out.getScore())
	count = count - 1




import heapq
dic={}
pic={}
h=[]
heapq.heapify(h)
for _ in range(int(input())):
	i,old,p,l,c,s=list(map(int,input().split()))
	new=p*50+l*5+c*10+s*20
	
	change=(new-old)+i*1.0/1000000000
	dic[change]=i
	pic[change]=new
	heapq.heappush(h,-change)
	

hi=heapq.nsmallest(5, h)
for i in hi:
	print(dic[-i],pic[-i])


if __name__=='__main__':
	n = eval(input())
	a = []
	new_z = {}
	for i in range(n):
		ids,z,p,l,c,s = list(map(int,input().split()))
		a.append(((p*50 + l*5 + c*10 + s*20 - z) , ids))
		new_z[ids] = p*50 + l*5 + c*10 + s*20
	a.sort(reverse=True)
	for i in range(5):
		print('{0} {1}'.format(a[i][1],new_z[a[i][1]]))


n = int(input());
final = [];
ids = [];
idChangeMap = {};
idNewMap = {};
for i in range(n):
	ns = list(map(int,input().split(' ')));
	ids.append(ns[0]);
	idNewMap[ids[i]] = ns[2]*50+ns[3]*5+ns[4]*10+ns[5]*20;
	idChangeMap[ids[i]] = idNewMap[ids[i]] - ns[1];
result = sorted(idChangeMap,reverse = True)
result = sorted(result,key=idChangeMap.get,reverse = True)
s = 0;
for i in result:
	if(s<5):
		print(str(i)+" "+str(idNewMap[i]));
	s+=1;


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

def main():
	import sys

	f = sys.stdin
	#f = open("roy-and-trending-topics-1.txt")
	
	N = int(f.readline())
	d1 = {}
	d2 = {}
	for dummy in range(N):
		_id, cur_z, p, l, c, s = list(map(int, f.readline().strip().split()))
		new_z = (50*p) + (5*l) + (10*c) + (20*s)
		if _id not in d1:
			d1[_id]=new_z
			d2[_id]= new_z - cur_z
	a=sorted(list(d2.items()), key=lambda x: (x[1], x[0]), reverse=True)[:5]
	for i in range(len(a)):
		print("%s %s" % (a[i][0], d1[a[i][0]]))
		
	
main()


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
import queue
q = queue.PriorityQueue()
N =int(input())
scores = {'P':50,'L':5,'S':20,'C':10}
for i in range(N):
	ID, Z, P, L, C, S= list(map(int,input().split()))
	current_score = P*scores['P'] + L*scores['L'] + S*scores['S'] + C*scores['C']
	q.put((Z-current_score,-ID,current_score))
	
	
for i in range(5):
	c = q.get()
	print(-c[1],c[2])




n = int(input())
max5 = [(-10**9-11, 0, 0) for _ in range(5)]
for _ in range(n):
	di, z, p, l, c, s = list(map(int, input().split()))
	nz = p * 50 + l * 5 + c * 10 + s * 20
	dz = nz - z
	min5 = min(max5)
	if (dz, di, nz) > min5:
		i = max5.index(min5)
		max5[i] = (dz, di, nz)
for x, y, z in sorted(max5, reverse=1):
	print(y, z)
		


def calculate_score(data):
	score = 0
	score += data[2] * 50
	score += data[3] * 5
	score += data[4] * 10
	score += data[5] * 20
	delta = score - data[1]
	return (delta, score) + data


def solve(list_of_data):
	return sorted(list_of_data, key=lambda element: (element[0], element[2]), reverse=True)


N = int(input())
result = []
for i in range(N):
	data = tuple(map(int, input().split(" ")))
	result.append(calculate_score(data))

solution = solve(result)[:5]

for sol in solution:
	print(sol[2], sol[1])


class Topics:
	def __init__(self,id1,changeScore,score):
		self.id1 = id1
		self.changeScore = changeScore
		self.newScore = score
	def getId(self):
		return self.id1
	def getScore(self):
		if self.newScore != None:
			return self.newScore
		else:
			return 
		return self.newScore
	def getChangeScore(self):
		return self.changeScore

class MinHeap:

	def __init__(self):
		self.items = [0]
		self.current_size = 0

	def insert(self,data):
		if (self.current_size == 0):
			self.current_size+=1
			self.items.append(data)
		else:
			self.items.append(data)
			self.current_size+=1
			self.percUp(self.current_size)

	def percUp(self,current_index):
		parent_index = current_index / 2
		while(parent_index >= 1 and self.items[parent_index].getChangeScore() <= self.items[current_index].getChangeScore()):
			if (self.items[parent_index].getChangeScore() == self.items[current_index].getChangeScore()):
				if (self.items[parent_index].getId() < self.items[current_index].getId()):
					temp = self.items[parent_index]
					self.items[parent_index] = self.items[current_index]
					self.items[current_index] = temp
			else:
				temp = self.items[parent_index]
				self.items[parent_index] = self.items[current_index]
				self.items[current_index] = temp
			current_index = parent_index
			parent_index = current_index / 2
			

	def printItems(self):
		for i in range(1,len(self.items)):
			print(self.items[i], end=' ')
		print()

	def size(self):
		return len(self.items)-1

	def isEmpty(self):
		return (self.current_size == 0)

	def delMin(self):
		min = self.items[1]
		self.items[1] = self.items[len(self.items)-1]
		self.items.pop()
		self.current_size-=1
		if not self.isEmpty():
			self.percDown(1)
		return min

	def percDown(self,parent_index):
		while (parent_index * 2) <= self.current_size:
			min_index = self.get_Min_Index(parent_index)
			if (self.items[parent_index].getChangeScore() <= self.items[min_index].getChangeScore()):
				if(self.items[parent_index].getChangeScore() == self.items[min_index].getChangeScore()):
					if (self.items[parent_index].getId() < self.items[min_index].getId()):
						temp = self.items[parent_index]
						self.items[parent_index] = self.items[min_index]
						self.items[min_index] = temp
				else:
					temp = self.items[parent_index]
					self.items[parent_index] = self.items[min_index]
					self.items[min_index] = temp
			parent_index = min_index


	def get_Min_Index(self,parent_index):
		lchild_index = 2 * parent_index
		rchlid_index = 2 * parent_index + 1
		if rchlid_index > self.current_size:
			return lchild_index
		else:
			if (self.items[lchild_index].getChangeScore() >= self.items[rchlid_index].getChangeScore()):
				if (self.items[lchild_index].getChangeScore() == self.items[rchlid_index].getChangeScore()):
					if (self.items[lchild_index].getId() > self.items[rchlid_index].getId()):
						return lchild_index
					else:
						return rchlid_index
				else:
					if (self.items[lchild_index].getChangeScore() > self.items[rchlid_index].getChangeScore()):
						return lchild_index
					else:
						return rchlid_index
			else:
				return rchlid_index
				# if (self.items[lchild_index].getChangeScore() < self.items[rchlid_index].getChangeScore()):
				# 	return lchild_index
				# else:
				# 	return rchlid_index

	def findMin(self):
		if not self.isEmpty():
			return self.items[1]
		else:
			return -1

heap = MinHeap()
for i in range(int(input())):
	id1 = 0
	oldScore = 0
	newScore = 0
	tps = [int(x) for x in input().strip().split(" ")]
	oldScore = tps[1]
	id1 = tps[0]
	if (tps[2] != 0):
		newScore+=(tps[2] * 50)
	if (tps[3] != 0):
		newScore+=(tps[3] * 5)
	if (tps[4] != 0):
		newScore+=(tps[4] * 10)
	if (tps[5] != 0):
		newScore+=(tps[5] * 20)
	changeScore = newScore - oldScore
	t = Topics(id1,changeScore,newScore)
	heap.insert(t)
count  = 5
while (count > 0):
	out = heap.delMin()
	print(out.getId(),out.getScore())
	count = count - 1




#########################
def heappush(heap, item):
	"""Push item onto heap, maintaining the heap invariant."""
	heap.append(item)
	_siftdown(heap, 0, len(heap) - 1)


def _siftdown(heap, startpos, pos):
	newitem = heap[pos]
	# Follow the path to the root, moving parents down until finding a place
	# newitem fits.
	while pos > startpos:
		parentpos = (pos - 1) >> 1
		parent = heap[parentpos]
		if newitem.change > parent.change:
			heap[pos] = parent
			pos = parentpos
			continue
		elif newitem.change==parent.change and newitem.id>parent.id:
			heap[pos] = parent
			pos = parentpos
			continue
		break
	heap[pos] = newitem


def heappop(heap):
	"""Pop the smallest item off the heap, maintaining the heap invariant."""
	lastelt = heap.pop()  # raises appropriate IndexError if heap is empty
	if heap:
		returnitem = heap[0]
		heap[0] = lastelt
		_siftup(heap, 0)
	else:
		returnitem = lastelt
	return returnitem


def _siftup(heap, pos):
	endpos = len(heap)
	startpos = pos
	newitem = heap[pos]
	# Bubble up the smaller child until hitting a leaf.
	childpos = 2 * pos + 1  # leftmost child position
	while childpos < endpos:
		# Set childpos to index of smaller child.
		rightpos = childpos + 1
		if rightpos < endpos and heap[childpos].change < heap[rightpos].change:
			childpos = rightpos
		if rightpos < endpos and (heap[childpos].change == heap[rightpos].change and heap[childpos].id < heap[rightpos].id):
			childpos = rightpos
		heap[pos] = heap[childpos]
		pos = childpos
		childpos = 2 * pos + 1
	# The leaf at pos is empty now.  Put newitem there, and bubble it up
	# to its final resting place (by sifting its parents down).
	heap[pos] = newitem
	_siftdown(heap, startpos, pos)


class trend:
	def __init__(self, id, change, z_change):
		self.id = id
		self.change = change
		self.z_change = z_change

	def __str__(self):
		return str(str(self.id) + " " + str(self.z_change))


######################
n = eval(input())
dic = {}
dic2 = {}
h = []
for i in range(0, n):
	values = [int(x) for x in input().split()]
	z = values[1]

	for j in range(1, len(values)):
		z_change = 50 * values[2]
		z_change = z_change + 5 * values[3] + 10 * values[4] + 20 * values[5]
		change = z_change - z
	t = trend(values[0], change, z_change)
	heappush(h, t)
#heappop(h)
for i in range(0, 5):
	print(heappop(h))