The king of ghosts is really disappointed when he sees that all the human beings on Planet Earth have stopped fearing the ghost race. He knows the reason for this. The existing ghost race has become really lazy and has stopped visiting Planet Earth to scare the human race. Hence, he decides to encourage the entire ghost race into scaring the humans by holding a competition. The king, however, never visits Planet Earth.
This competition will go on for N days. Currently, there are a total of M ghosts (apart from the king) existing in the ghost race such that :
The youngest ghost is 1 year old.
The oldest ghost is M years old.
No two ghosts have the same age.
The age of each and every ghost is a positive integer.
On each day of the competition, ghosts have to visit Planet Earth to scare people. At the end of each day, a "Ghost of the Day" title is awarded to the ghost who scares the most number of humans on that particular day. However, the king of ghosts believes in consistency. Once this title has been given, the ghost who has won the most number of such titles until that particular moment is presented with a "Consistency Trophy". If there are many such ghosts, the oldest among them is given the trophy. Note that this "Title Giving" and "Trophy Giving" happens at the end of each day of the competition.
You will be given the age of the ghost who won the "Ghost of the Day" title on each day of the competition. Your job is to find out the age of the ghost who was awarded with the "Consistency Trophy" on each day of the competition.
Input
The first line consists of 2 space separated integers N and M. The next line consists of N space separated integers such that the i^th integer denotes the age of the ghost who was awarded with the "Ghost of the Day" title on the i^th day of the competition.
Output
Print N lines. The i^th line should contain 2 space separated integers such that the first integer denotes the age of the ghost who was awarded with the "Consistency Trophy" on the i^th day and the second integer denotes the number of "Ghost of the Day" titles won by this ghost until the end of the i^th day of the competition.
Constraints
1 ≤ N ≤ 10^5
1 ≤ M ≤ 10^9
SAMPLE INPUT
7 5
1 3 1 3 2 2 2
SAMPLE OUTPUT
1 1
3 1
1 2
3 2
3 2
3 2
2 3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to process N days, each day we get an age of the ghost who won the "Ghost of the Day" title. At the end of each day, we need to output: the age of the ghost who gets the "Consistency Trophy", and the number of titles that ghost has won so far.
Definition of Consistency Trophy: Once the title is given (i.e., after we know today's winner), the ghost who has won the most number of such titles (i.e., total count up to that day) is given the trophy. If tie, the oldest among them gets it.
So we need to maintain the counts of titles for each ghost age (some ages may not appear until later; we have M up to 1e9, but only N up to 1e5 distinct ages appear, so we can map ages to counts). Each day after updating the count for the day's winner, we need to find the ghost with highest count. If multiple, choose the one with maximum age. Output its age and its count.
This is a classic problem of maintaining a multiset or priority queue of (count, age) and being able to update one element and query the maximum according to custom comparator: first by count descending, then age descending. Since we only increment counts (no decrements), we can maintain a max-heap that stores the current count and age for each ghost, but after updating a ghost, we can push a new entry with its updated count. When we query the top, we need to ensure that the entry corresponds to the current count for that age. We can lazily pop stale entries. Since we only increment counts, each ghost will have as many pushes as its number of wins (plus maybe initial zero? but we can start with counts zero for all ghosts? Actually, ghosts initially have zero titles. But do we need to consider all M ghosts? M could be up to 1e9, cannot store all. However, the trophy goes to ghost with most titles. At start, all ghosts have zero titles. But we need to output after each day. Before any day, no trophy? But we start from day 1. After day 1, we have counts for the winner=1, and others are zero. So the ghost with most titles could be the winner (count=1) vs all others count=0. So max count=1, and if tie? there could be multiple ghosts with count 1? Actually only the winner has count 1, others are 0. So winner is the unique max. So we only need to consider ghosts that have appeared at least once? But what about ties with zero? If no ghost has titles (before any day), but after day 1 we have at least one with count 1, so fine. But after some days, it's possible that multiple ghosts have same count>0, but if all counts are zero, not relevant. So we can ignore ghosts that have never won any title; they will have count 0, but they are never the max unless all counts are zero, which can't happen after day 1. However, there is a nuance: what if after day 1, the winner is age X, but there might be other ghosts with count 0 that have age > X? But max count is 1 > 0, so X is the max. So indeed only ghosts with count >0 are candidates. As days go on, we only need to track those that have at least one win. But note that a ghost that never wins could become the oldest among a tie if they have the same count as winners? But if they have count=0 and some winners also have count=0? That could happen only before any wins, but after first win, count 0 is less than 1, so not tie. Later, if some ghost has count =0, it's less than any positive count, so cannot be max. So indeed we only need to consider ghosts that have at least one title. So we can maintain a hash map: age -> current count.
Now we need to efficiently get the max (count, age) after each update. Since N up to 1e5, we can use a balanced BST (like a TreeSet in Java) or a heap with lazy deletion. Let's think about using a max-heap: store entries (-count, -age) for min-heap? Actually we need custom comparator: first by count descending, then age descending. So we can store as (count, age) and define a comparator: (a, b) => if a.count != b.count then b.count - a.count else b.age - a.age. We'll use a max-heap. However, when we increment a ghost's count, we push its new state (new count, age) into the heap. Then when we peek, we need to check if the top entry's count matches the current count for that age (i.e., it's not stale). If not, we pop until we get a valid top. Since each push results from an increment, total pushes = total updates = N (since each day increments exactly one ghost). So total pushes O(N). Each pop also at most O(N) overall because each entry is popped at most once. So heap with lazy deletion works fine.
But we need to consider initial state: before any increments, we might have no entries? But after first increment, we push (1, age). Then top will be (1, age). So okay.
Thus algorithm:
Initialize:
counts = dict (e.g., unordered_map in C++, HashMap in Java, dict in Python) to store age -> current count.
heap = priority queue (max-heap) with custom comparator: compare by count descending, then age descending.
For each day i from 1 to N:
read age A
if A not in counts: counts[A] = 0
counts[A] += 1
push (counts[A], A) onto heap
// now get current best
while True:
(cnt, age) = heap.top()
// check if counts[age] exists and equals cnt (also if counts[age] is maybe missing? but we only push for ages we've seen)
if counts.get(age) == cnt:
break
else:
pop heap
// now (cnt, age) is the ghost with highest count (and oldest if tie)
output age and cnt
But careful: The comparator should be first by count descending, then age descending. That ensures that if multiple have same count, the older (higher age) is chosen.
Now test with sample:
N=7 M=5
Day1: 1 -> counts{1:1}, push (1,1). heap top = (1,1) valid -> output 1 1.
Day2: 3 -> counts{1:1, 3:1}, push (1,3). heap contains (1,1) and (1,3). Which is top? Since counts equal, compare age: higher age gets precedence? Actually we need oldest among highest count, so age descending. So (1,3) should be on top (since age 3 > 1). So heap top (1,3) valid -> output 3 1.
Day3: 1 -> counts{1:2, 3:1}, push (2,1). heap now (2,1) and (1,3) etc. Top is (2,1) -> output 1 2.
Day4: 3 -> counts{1:2, 3:2}, push (2,3). heap top? Two entries with count=2: (2,1) and (2,3). Age descending => (2,3) on top. So output 3 2.
Day5: 2 -> counts{2:1}, also 1:2, 3:2? Actually we need to increment 2: counts{2:1}. push (1,2). But now top should be: counts: 1:2, 3:2, 2:1. So max count=2, among 1 and 3, oldest is 3. So output 3 2. Check: heap now contains (2,1), (2,3), (1,2), etc. Top should be (2,3) (valid) -> output 3 2.
Day6: 2 -> counts{2:2}, push (2,2). Now we have (2,1), (2,3), (2,2). Highest count=2. Age descending: 3 > 2 > 1. So top (2,3) valid -> output 3 2.
Day7: 2 -> counts{2:3}, push (3,2). Now highest count=3 for age 2. But age 2 has count 3, while others have count 2. So top (3,2) valid -> output 2 3. Good.
So matches sample.
Now consider complexity: O(N log N) with heap operations.
Edge cases:
N=1: just output that age and 1.
Multiple ghosts with same count, oldest age.
M up to 1e9 but we don't need it except maybe for reading? The input gives ages, they are within 1..M presumably, but we don't need to preallocate anything.
One nuance: There might be ghosts with count 0 but age huge? However they are not max because count is 0, but if all ghosts have count 0? That cannot happen after day 1 because we always increment a ghost, so at least one ghost has count>=1. So we can ignore them.
But what if after some day, the ghost with max count is the one we just updated, and it's also the oldest among ties. The heap approach handles.
Thus solution is straightforward.
We need to implement in Python.
Python's heapq is min-heap. To simulate max-heap we push negative values or use custom comparator via tuple with reversed order. Since we need to compare by count descending, then age descending, we can push (-count, -age) or ( -count, -age )? Actually if we push (-count, -age), then the heap's natural order (ascending) will give the smallest -count (i.e., largest count) and if ties, smallest -age (largest age). That works: For (cnt, age), we push (-cnt, -age). Then heapq.heappop gives the smallest tuple. So to get the max according to original, we peek at heap[0] but that gives the smallest (-cnt, -age). So we need to invert again when retrieving: cnt = -heap[0][0]; age = -heap[0][1]. And checking validity: we need to compare with counts[age]? Actually we need to compare the stored cnt (which is negative of the count we pushed) with current count? We'll store the actual count as negative? Let's define: we push ( -count, -age ) onto heap. So heap is min-heap by first element then second. So the top is the one with smallest -count (largest count), and if tie, smallest -age (largest age). So it matches our desired max. So we can use that.
But then for lazy deletion we need to check if the current count for that age equals the negative of the first element? Actually we have stored the count at the time of push. So we can store also the age (positive) maybe to facilitate checking? Better: push tuple ( -count, -age, age )? Actually we can keep -count, -age and also store age separately? But we can extract age from -age: age = -neg_age. So after pop we can compute actual age = -neg_age. But careful: we need to know the current count for that age. We'll maintain a dictionary counts mapping age -> current count. So after getting heap top, we compute actual_age = -neg_age, then check if counts.get(actual_age) == -neg_count? Actually neg_count is -count; so -neg_count = count. So we check: if counts[actual_age] == -neg_count. If not, stale.
So:
import heapq
heap = []
counts = {}
for each day:
a = int(...)
counts[a] = counts.get(a, 0) + 1
heapq.heappush(heap, (-counts[a], -a))
while True:
neg_cnt, neg_age = heap[0]
cnt = -neg_cnt
age = -neg_age
if counts.get(age, 0) == cnt:
break
else:
heapq.heappop(heap)
print(age, cnt)
But need to be careful: In case the heap becomes empty? That shouldn't happen because we always push after updating, and there will always be at least one element. However, consider the case where we push a new entry, then pop stale ones, but if all stale? That can't happen because we just pushed a valid entry for the updated age, so at least that one is valid. So eventually we'll find a valid top.
Time complexity: O(N log N) due to pushes and pops.
We must handle up to 1e5 days, Python should be fine.
Now consider memory: heap can contain up to N entries (maybe up to N if no stale removals? Actually each push adds one, and we may have stale entries that haven't been popped yet. In worst-case scenario, suppose we always update the same ghost, then each push (new count) will replace previous? For same ghost, the older entries become stale, but we will pop them when they reach top? Let's see: heap top is the largest count. If we keep updating the same ghost, each push gives a higher count, so older entries with lower count are not at top; they may remain in heap forever, never being popped because the top will always be the latest highest count. But eventually when that ghost stops winning, and another ghost becomes the max, then the heap top will be from another ghost; the stale entries for the first ghost will be popped only when they become top, which may happen if that ghost's count is still the highest? Actually if we have stale entries with lower counts, they will never be at top because there's always a higher count entry from that same ghost (the latest) or from others. But we need to ensure that we eventually clean them up to avoid infinite memory? However, we only check stale when an entry is at top. If we never pop stale entries that are not at top, they accumulate. In worst-case scenario, we could have O(N) stale entries if we keep updating the same ghost? Let's simulate: Ghost A gets wins on days 1 to N. We push (1, A), (2, A), ..., (N, A). All entries except the latest are stale. The heap will contain N entries. The top will be the one with highest count, which is (N, A). That's valid (if we check counts[A]==N). The others remain in heap but are never top because they have lower count. So we never pop them. Over N days, heap size grows to N. That's fine because N=1e5, memory is okay. If we had many distinct ghosts, we push each update, and each ghost may have multiple pushes. Total pushes = N, so heap size at most N. That's fine. So we don't need to worry about cleaning stale entries not at top. However, if we want to minimize, we could pop whenever we see stale, but we only check top. That's acceptable.
Thus solution is simple.
Now let's test with a scenario where two ghosts tie and we need oldest: Suppose counts: ghost1 age=5 count=2, ghost2 age=10 count=2. Oldest is 10. Our heap pushes (-2, -10) and (-2, -5). The heap ordering: compare (-2, -10) vs (-2, -5). Since -10 < -5, (-2, -10) is smaller, so it's top. That yields age=10. Good.
Now consider negative ages? Ages are positive, fine.
Thus answer is done.
Now check constraints: M up to 1e9, but ages in input are within 1..M? The input says "the age of the ghost who won ...". It's implied they are between 1 and M inclusive. But we don't need M anyway.
Now output: Each line two integers: age and count.
Edge case: If there is a tie for max count, but the oldest among them has count > others, obviously, but we need to output that ghost's count (which is the max count). So that's straightforward.
Implementation details: Read N, M from input, then read list of N integers. For each, process as described.
But wait: The problem statement: "Print N lines. The i^th line should contain 2 space separated integers such that the first integer denotes the age of the ghost who was awarded with the 'Consistency Trophy' on the i^th day and the second integer denotes the number of 'Ghost of the Day' titles won by this ghost until the end of the i^th day of the competition." So we need to output after each day.
Now let's test with more examples manually:
Example: N=3 M=100: [5,5,5] -> day1: (5,1); day2: count 5:2 -> max count=2, age=5, tie? only one; day3: (5,3). So outputs 5 1, 5 2, 5 3.
Example: N=3 M=100: [1,2,1] -> day1: 1,1; day2: counts:1=1,2=1; tie count=1, oldest is 2? age 2 >1 -> output 2 1; day3: counts:1=2,2=1 -> output 1 2.
Seems correct.
Now consider scenario where after a day, the max count is from multiple ages, and the oldest is not the one just updated. That's fine.
Now, let's confirm the heap method works when there are stale entries from the same ghost with lower count: They won't be at top because a higher count entry for the same ghost exists. But could there be a scenario where the top entry is stale but still has the highest count? If we update a ghost, we push a new entry with a higher count. The old entry has lower count, so it's not the highest count (unless the new count is the same? But we increment by 1, so new count > old count). So old entries will never be the highest count for that ghost. But could an old entry be the top if there is a tie with other ghosts? Example: Ghost A had count 5, we push (5,A). Then we update A to 6, push (6,A). Now the heap contains (5,A) and (6,A). The top is (6,A) because count 6 >5. So (5,A) is not top. So it won't be popped unless it becomes top later, but it can't become top because we have (6,A) which has higher count, and any other ghost's count might be less than 6; (5,A) cannot be top because (6,A) is always higher. So safe.
Now consider scenario where we update a ghost, but later another ghost surpasses it, and then the first ghost becomes stale with its latest count (say 7) but then another ghost has count 8, so top is (8, other). The first ghost's entry (7) might still be in heap and could be top if we pop the (8) after that ghost's count changes? Actually if we later update the other ghost to 9, we push (9). Then (8) becomes stale. The heap top will then be (9, other) (if it's the highest). The stale (8) may become top if (9) is popped? But we pop only when checking top; we'll pop (9) if it's not valid? Actually (9) is valid. So top is (9). So (8) remains not top. So stale entries for other ghosts also won't become top unless they have the highest count and become stale due to further increments. But if we have a ghost with count 8, then we increment it to 9, we push (9) and the (8) becomes stale. Now the heap top may be (9) (valid) or maybe another ghost has higher? If (9) is the highest, top = (9) (valid). The stale (8) is not top. So it remains. So overall, we only remove stale entries that reach the top. In some worst-case scenario, if we have many updates to the same ghost, we will have many stale entries that never become top (because the latest highest count always supersedes them). However, this is fine as total entries = N. So memory O(N).
One more nuance: When we push a new entry for a ghost, we also need to ensure that the heap top is correct. Since we push and then clean only top until valid, it's fine.
Now consider: Could the heap be empty after cleaning? Suppose we have a case where we push an entry, then we pop until we find a valid entry. Since we just pushed a valid entry for the updated age, at least that entry is in heap. But could it be that we accidentally popped it? In our cleaning loop, we peek top, and if it's stale we pop. But if the newly pushed entry is not the top (maybe there is a higher count from another ghost), then the top might be something else. If that top is stale, we pop it and continue. Eventually we might pop all stale entries until we find a valid top. That valid top could be the newly pushed entry or another valid entry. So we won't pop the new entry because it's not top yet. So at least one valid entry remains. So fine.
Thus implementation is straightforward.
Now, I'd like to discuss potential pitfalls: The M is given but we ignore it. The ages could be up to 1e9, but Python dict can handle it.
Now complexity: O(N log N). N up to 1e5, log N ~ 17, fine.
Now think about using an OrderedDict or something else? Not needed.
Edge cases: If input contains ages outside 1..M? Not expected.
Now, we might also think about using a TreeMap with custom comparator, but Python lacks built-in sorted map. Could use sortedcontainers but not allowed. So heap is best.
Now I'll produce final solution in Python.
Let's outline:
def solve():
import sys, heapq
data = sys.stdin.read().split()
if not data: return
it = iter(data)
N = int(next(it))
M = int(next(it)) # not used
heap = []
counts = {}
out_lines = []
for _ in range(N):
a = int(next(it))
# update count
count = counts.get(a, 0) + 1
counts[a] = count
heapq.heappush(heap, (-count, -a))
# clean top
while True:
neg_cnt, neg_age = heap[0]
cnt = -neg_cnt
age = -neg_age
if counts.get(age, 0) == cnt:
break
heapq.heappop(heap)
out_lines.append(f"{age} {cnt}")
sys.stdout.write("\n".join(out_lines))
Now test with sample:
Input:
7 5
1 3 1 3 2 2 2
Our code should output as sample.
Let's manually run:
Initialize heap=[], counts={}
Day1: a=1, count=1, push (-1,-1). heap top: (-1,-1). Check counts.get(1,0)=1 equals cnt=1 -> age=1 cnt=1, output.
Day2: a=3, count=1, push (-1,-3). heap now [(-1,-3), (-1,-1)]? Actually heap after push: heapq maintains min-heap order, so top will be the smallest: (-1,-3) vs (-1,-1) -> (-1,-3) is smaller because -3 < -1. So top = (-1,-3). Check: age=3, counts.get(3)=1 equals cnt=1. Output 3 1.
Day3: a=1, count=2, push (-2,-1). heap now [(-1,-3), (-1,-1), (-2,-1)]? The heap order may be: (-1,-3), (-1,-1), (-2,-1). Top is (-2,-1) because -2 < -1. Actually we need to think: After push, heap heapify; the smallest tuple is (-2,-1) since -2 is smaller than -1. So top = (-2,-1). Check age=1, counts[1]=2 equals 2 -> valid. Output 1 2.
Day4: a=3, count=2, push (-2,-3). heap: top? The smallest tuple might be (-2,-3) vs (-2,-1) etc. (-2,-3) is smaller because -3 < -1, so top = (-2,-3). Check age=3, counts[3]=2 -> valid. Output 3 2.
Day5: a=2, count=1, push (-1,-2). heap now top? The smallest: we have entries: (-2,-3), (-2,-1), (-1,-3), (-1,-1), (-1,-2). The smallest is (-2,-3) (since -2 < -1). That's still top, but we need to check if stale? counts[3]=2, and top's cnt = -(-2) =2, so valid. Output 3 2.
Day6: a=2, count=2, push (-2,-2). heap top? We have (-2,-3) still the smallest? Compare (-2,-3) vs (-2,-2): -3 < -2, so (-2,-3) remains smallest. Valid. Output 3 2.
Day7: a=2, count=3, push (-3,-2). heap top? Now smallest tuple: (-3,-2) because -3 < -2. So top = (-3,-2). Check age=2, counts[2]=3 equals 3 -> valid. Output 2 3.
Matches.
Now consider a tricky case: Suppose we have many ghosts with same count, and we need oldest. Since we use (-cnt, -age), the heap order yields the oldest (largest age) because -age is smaller for larger age? Wait: For ages: larger age gives more negative? Actually age positive. For age=10, -age = -10; age=5, -age = -5. Since -10 < -5, larger age yields smaller tuple when cnt ties. So indeed the oldest appears at top. Good.
Now what about if counts are not updated properly? For example, if we have a ghost with age 1000000000, that's fine.
Now, we should also consider that we might have to output large numbers, but that's fine.
Now, might there be an issue with reading input using sys.stdin.read? It's fine.
Now, as a final check, let's test some random scenarios programmatically (imagine). But we trust.
Now, I'll produce final answer with explanation.
But the problem might be from CodeChef? Possibly there is a twist: The king never visits Earth, so no impact. M is the number of ghosts; but we don't need it except to bound ages? But maybe we need to consider that there are ghosts that never won but could be tied for zero? Not after first day. However, consider the scenario where the first day winner is age 1, but there are older ghosts with age M that have zero titles. The maximum count is 1, and zero titles are less, so they are not considered. So it's fine.
But what about the initial state before any title is awarded? Not required.
Thus solution stands.
Now, we can also think about using a Counter and a sorted list, but heap is simpler.
Now produce final answer.
But maybe there is a nuance: The Consistency Trophy is given after the title is given each day. And if there are many ghosts with the most titles, the oldest among them gets it. That's what we implemented.
One more nuance: "The king of ghosts never visits Planet Earth." That's irrelevant.
Now final code.
Let's also consider performance: Using sys.stdin.read and split is efficient.
Now answer should be in Python.
But sometimes in competitions they expect use of fast I/O. We'll do that.
Now let's produce the final solution with explanation.
I'll write solution in function solve() and call it.
Now provide final answer.
But the problem's sample output shows each line with two integers separated by space, no extra spaces.
We'll output as described.
Now, final answer. ```python
import sys
import heapq
def solve() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it)) # not used, only to read input
heap = [] # min‑heap storing (-count, -age)
cnt = {} # current counts for ages that have appeared
code
out_lines = []
for _ in range(n):
age = int(next(it))
# update count for this age
cur = cnt.get(age, 0) + 1
cnt[age] = cur
heapq.heappush(heap, (-cur, -age))
# lazy deletion: discard stale entries from the top
while True:
neg_c, neg_a = heap[0]
c = -neg_c
a = -neg_a
if cnt.get(a, 0) == c:
break
heapq.heappop(heap)
out_lines.append(f"{a} {c}")
sys.stdout.write("\n".join(out_lines))