Polycarpus got an internship in one well-known social network. His test task is to count…
- User
Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarpus has already accidentally removed the user IDs corresponding to the requests from the database. Thus, it is now impossible to determine whether any two requests are made by the same person or by different people.
But wait, something is still known, because that day a record was achieved — M simultaneous users online! In addition, Polycarpus believes that if a user made a request at second s, then he was online for T seconds after that, that is, at seconds s, s + 1, s + 2, ..., s + T - 1. So, the user's time online can be calculated as the union of time intervals of the form [s, s + T - 1] over all times s of requests from him.
Guided by these thoughts, Polycarpus wants to assign a user ID to each request so that:
- the number of different users online did not exceed M at any moment,
- at some second the number of distinct users online reached value M,
- the total number of users (the number of distinct identifiers) was as much as possible.
Help Polycarpus cope with the test. Input
The first line contains three integers n, M and T (1 ≤ n, M ≤ 20 000, 1 ≤ T ≤ 86400) — the number of queries, the record number of online users and the time when the user was online after a query was sent. Next n lines contain the times of the queries in the format "hh:mm:ss", where hh are hours, mm are minutes, ss are seconds. The times of the queries follow in the non-decreasing order, some of them can coincide. It is guaranteed that all the times and even all the segments of type [s, s + T - 1] are within one 24-hour range (from 00:00:00 to 23:59:59).
Output
In the first line print number R — the largest possible number of distinct users. The following n lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to R. The requests of the same user must correspond to the same identifiers, the requests of distinct users must correspond to distinct identifiers. If there are multiple solutions, print any of them. If there is no solution, print "No solution" (without the quotes).
Examples
Input
4 2 10 17:05:53 17:05:58 17:06:01 22:39:47
Output
3 1 2 2 3
Input
1 2 86400 00:00:00
Output
No solution
Note
Consider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in that case all these users would be online, for example, at 17:06:01. That is impossible, because M = 2. That means that some two of these queries belonged to the same user. One of the correct variants is given in the answer to the sample. For it user 1 was online from 17:05:53 to 17:06:02, user 2 — from 17:05:58 to 17:06:10 (he sent the second and third queries), user 3 — from 22:39:47 to 22:39:56.
Assistant
import heapq n, M, T = map(int, input().split()) times = [] for _ in range(n): time_str = input().strip() hh, mm, ss = map(int, time_str.split(':')) s = hh * 3600 + mm * 60 + ss e = s + T - 1 times.append((s, e)) heap = [] user_ends = {} assignments = [] current_user_id = 1 max_concurrent = 0 for s, e in times: # Clean up the heap from outdated entries and those ending before s while heap: end_time, user = heap[0] if user_ends.get(user, -1) != end_time: heapq.heappop(heap) continue if end_time < s: heapq.heappop(heap) else: break current_concurrent = len(heap) new_concurrent = current_concurrent if current_concurrent < M: # Assign to a new user new_user = current_user_id assignments.append(new_user) user_ends[new_user] = e heapq.heappush(heap, (e, new_user)) current_user_id += 1 new_concurrent = current_concurrent + 1 else: # Merge with the earliest ending user valid_entry = False while heap: end_time, user = heapq.heappop(heap) if user in user_ends and user_ends[user] == end_time: valid_entry = True break if not valid_entry: # This case should not occur as per problem constraints print("No solution") exit() new_end = max(end_time, e) user_ends[user] = new_end heapq.heappush(heap, (new_end, user)) assignments.append(user) new_concurrent = current_concurrent if new_concurrent > max_concurrent: max_concurrent = new_concurrent if max_concurrent < M: print("No solution") else: print(current_user_id - 1) for user_id in assignments: print(user_id)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.