# Need help in solving problem ICPC Trainer DS Heaps

This is a question of July long challenge 2017. I tried hard but i get tle in subtask #2. I know my algorithm is too slow.First i simply sort the array depending upon priority. But then i realized it will take O(NLOGN) time. But after seeing the editorial i find heap is most suitable. Then i studied heaps and find it will take less time. Since it will take O(n) time to build heap and for each time it will take O(logn) to max heapify. So it will be more better.But still i can’t find the suitable way to assign the lectures to suitable day.I know the priority trainer will first be considered but i can’t find the suitable way to allocate days to trainer.Its my first question on codechef forum hope i will get the answer of my question.The link to my solution is https://www.codechef.com/viewsolution/14522626 .Please let me know if u don’t get the question. I have added //complicated comment from there after i need help. Thank YOU.

1 Like

``````import Queue
import heapq

for _ in range(input()):
n,d = map(int, raw_input().split())
daywisePeople = sorted([map(int, raw_input().split()) for i in range(n)])
peopleIndex = 0
for dayIndex in range(1,d+1):
while peopleIndex < n and daywisePeople[peopleIndex][0] <= dayIndex:
peopleIndex += 1

continue

if topPriorPerson[1] != 1:
topPriorPerson[1] -= 1