We need to solve the problem: given n distinct minutes within [1, m] (m can be up to 1e9, n up to 2e5). We have working days of length m minutes. On each day, we can take coffee breaks at certain minutes (the times a_i we need to cover). But within a day, any two breaks must be at least d minutes apart (i.e., the difference in minutes must be at least d). And between days, we have more than d minutes between the end of one working day and the start of the next (so breaks on consecutive days are not constrained relative to each other). We need to assign each break to a day such that, for each day, the set of times assigned to that day (which are within [1,m]) satisfy pairwise distance >= d. Minimize the number of days used. Then output the day index for each break.
This is essentially a scheduling/coloring problem. The constraints within a day: if we assign breaks at times that are too close (difference < d) to the same day, that's invalid. So each day can contain a set of times that are "d-separated". The goal is to partition the n times into as few such sets as possible, where each set is a subset of the times such that any two times in the set are at least d apart. Since the days are identical and infinite, the minimum number of days equals the size of the largest "clique" where times are mutually too close? Actually, it's similar to graph coloring: construct graph where vertices are breaks, and edges between breaks if |a_i - a_j| < d. Then we need to color vertices with minimum number of colors such that adjacent vertices have different colors. The graph is an interval graph? The condition: an edge exists if the absolute difference < d. This is a unit interval graph (since intervals of length d-1? Actually, each break is a point; edge if distance < d; that's the complement of a comparability? But it's essentially a graph defined by distance threshold). It's a "threshold graph"? Actually it's a unit disk graph on a line, which is an interval graph? Since points on a line with distance < d, the graph is called a "unit interval graph" because we can represent each vertex by an interval [a_i, a_i + d-1] and then two intervals overlap iff distance < d? Let's check: intervals I_i = [a_i, a_i + d-1]. Overlap condition: they intersect if max(a_i, a_j) <= min(a_i+d-1, a_j+d-1) => |a_i - a_j| <= d-1, i.e., distance < d. So edges represent overlapping intervals. Indeed, graph of overlapping intervals is an interval graph, and unit length intervals? Actually intervals have length d-1, but they're not necessarily unit length; but they all have same length d-1, so it's a unit interval graph (also called proper interval graph). So the graph is a unit interval graph, which is perfect. Then the chromatic number equals the size of the maximum clique. And maximum clique in an interval graph can be found by sorting intervals and looking for maximum number of intervals overlapping at any point. Since intervals are all length d-1 (or half-open? For strict inequality < d, we need intervals of length d-1 inclusive? Actually distance < d means |a_i - a_j| <= d-1 if integer minutes. So yes, overlap when intervals [a_i, a_i+d-1] intersect. So we can compute max number of intervals that cover any single point. That is the max clique size, and equals the minimum number of colors needed.
Thus the answer for minimum days is the maximum number of breaks such that any two among them have distance < d? Actually the clique is a set where pairwise distance < d (so they all conflict). So we need to find the maximum cardinality subset of times where all pairwise distances are < d? Wait, but for intervals, a clique corresponds to all intervals overlapping at a common point. That is a set where there exists some x such that for all i in set, a_i <= x <= a_i + d-1. This implies that the maximum difference between any two a_i in the set is <= d-1? Actually if they all contain x, then the max a_i <= x <= min a_i + d-1 => max a_i - min a_i <= d-1. So indeed all pairwise distances are <= d-1 (< d). Conversely, if a set of points has all pairwise distances < d, then they are all within an interval of length d-1 (just take min and max: if max - min < d, then all points lie within [min, max], but to guarantee a common overlap point? For unit intervals of length d-1, if max - min <= d-1, then there exists x = min + (d-1)/2? Actually we need to see: For intervals [a_i, a_i+d-1], they all overlap if and only if max a_i <= min a_i + d-1, which is exactly max - min <= d-1. So indeed a set of points with max - min <= d-1 forms a clique. So the maximum clique size is the maximum number of points that can be contained in any interval of length d-1. That is a classic problem: given sorted times, sliding window of length d-1 (or less than d?) Actually condition: for any two points in same day, we need distance >= d. So conflicts if distance < d. So maximum number of mutually conflicting points is the maximum number of points that are all within a window of size d-1? Wait: If distance < d, they conflict. So a set of points that are all pairwise conflicting means for any two, distance < d. This is stronger than just being in a window of size d-1? Actually if the maximum distance between any two is less than d, then they all fit within an interval of length less than d. But if the window size is exactly d-1, the maximum difference can be d-1, which is < d. So yes, being within an interval of length d-1 ensures all pairwise distances are <= d-1 < d. Conversely, if all pairwise distances are < d, then the max-min < d (since the max distance equals max-min). So they are within an interval of length less than d, i.e., <= d-1 (since integer minutes). So it's equivalent. So we need to find the maximum number of points in any contiguous segment of length d-1 (or technically less than d) among the sorted a's. That is easy: sort a, then sliding window: maintain left pointer; for each right, while a[right] - a[left] >= d, left++. Then max window size is answer. That gives min days = k.
Now we also need to assign each break to a day (color) 1..k, such that within each day, any two breaks have distance >= d. This is essentially coloring a unit interval graph with k colors, where k equals the max clique size. Since it's a perfect graph, such a coloring exists and can be constructed greedily by scanning sorted points and assigning each point to the first available color that does not conflict with the most recent points of that color? Or we can use a simple scheduling algorithm: assign each break a day index based on its order modulo k? But careful: The condition is that if two breaks are within distance < d, they must be on different days. So if we sort times and assign them in order, we could assign each break to a color based on its index mod k, but is that always valid? Let's test with example 1: n=4, m=5, d=3, a=[1,2,3,5] sorted. Window: check differences: any two within <3 conflict. Sorted: 1,2,3,5. max clique? Check window of length 2 (d-1=2): points 1,2,3 are within distance 2? Actually 3-1=2 <=2, so 3 points within interval length 2 => max window size=3? But answer says min days = 3. Yes, max clique size=3. So k=3. If we assign modulo 3: index order: 1->1,2->2,3->3,5->1. That gives days: 1,2,3,1. Check: day1 has breaks at 1 and 5: distance 4 >=3, ok. day2: break at 2; day3: break at 3. Works. But is modulo assignment always valid? Let's test example 2: n=10, m=10, d=1. d=1 means any two distinct breaks must be at least 1 minute apart, which is always true since minutes are distinct integer? Actually distance >=1 means any two different minutes are okay (since distinct integers, difference at least 1). So there is no conflict. So max clique? window length d-1=0? Actually condition distance <1 means distance 0 only for same minute, but distinct so no edges. So max clique size=1? Actually sliding window with d=1: condition a[right]-a[left] >=1 => as long as they are distinct, they are all >=1, so left will move to right-1? Let's compute: sorted times: 1,2,3,4,5,6,7,8,9,10. d=1: conflict if distance <1 => none. So max window size is 1 (since condition for conflict is distance <1, which is impossible for distinct points, so any two points are not in conflict, so max clique =1? Actually clique is set of mutually conflicting points. Since no two conflict, max clique size =1. So min days =1? But output says 2 days. Wait, conflict condition is: within a day, at least d minutes must pass between any two coffee breaks. For d=1, that means consecutive breaks must be at least 1 minute apart. Since minutes are integers, distinct minutes are always at least 1 apart. So indeed all breaks can be on the same day. But output says 2 days. Why? Let's read the problem statement again: "for the given coffee break that is going to be on minute a_i, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least d minutes pass between any two coffee breaks. Monocarp also wants to take these n coffee breaks in a minimum possible number of working days". For d=1, any set of distinct minutes satisfy >=1 minute difference, so all can be on one day. So why answer is 2? Something is off.
Let's examine second example input: "10 10 1" and list: 10 5 7 4 6 3 2 1 9 8. Sorted: 1,2,3,4,5,6,7,8,9,10. d=1. The output says days=2. Wait maybe because each working day length m=10, and coffee breaks must be taken within that day's minute range? Actually the day is from minute 1 to m inclusive? It says "His working day lasts exactly m minutes." So on each day, the minutes go from 1 to m. But the coffee break times a_i are given as minutes (presumably within a day). But they are not tied to any specific day; we choose which day to take that break at that minute. However, if we assign all breaks to day 1, then on that day we would have breaks at minutes 1,2,...,10. That's fine because they are all at different minutes, and d=1 is satisfied (since difference between any two is at least 1). So why would they need two days? Possibly because the problem also says "take into account that more than d minutes pass between the end of any working day and the start of the following working day." That only matters for breaks across days? It says that more than d minutes pass between the end of any working day and the start of the following working day, meaning we don't have to worry about constraints between breaks at the end of one day and the start of the next. So that's not forcing extra days.
Maybe I misinterpret: The condition "every day at least d minutes pass between any two coffee breaks" might be interpreted as the time between the end of one coffee break and the start of the next must be at least d. If coffee breaks last exactly one minute, then perhaps the requirement is that the start times must be separated by at least d minutes? Typically, if a break lasts one minute, then if one break occurs at minute x, it occupies minute x. Then the next break must start at minute y such that |y - x| >= d? Or maybe if they are on the same day, the gap between the end of one and the start of the next must be at least d. Since each break lasts 1 minute, that means |(y) - (x+1)|? Or the problem says "each coffee break lasts exactly one minute" and "at least d minutes pass between any two coffee breaks." I think it means the start times must be at least d apart, because they talk about minutes passing between breaks. So if one break at minute 1, it occupies minute 1, then there must be at least d minutes between that break and the next, meaning the next cannot start before minute 1 + d? But the problem statement: "for the given coffee break that is going to be on minute a_i, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least d minutes pass between any two coffee breaks." It doesn't clarify whether it's from start to start or from end to start. Typically, when we say "between any two coffee breaks", we consider the time interval between the two breaks. If each break lasts 1 minute, the natural interpretation is that the breaks should not overlap and there should be at least d minutes from the end of one to the start of the next? However, they explicitly say "each coffee break lasts exactly one minute", so if two breaks are on the same minute, they'd overlap, which is impossible. So they must be on distinct minutes. Additionally, they want at least d minutes passing between them. If one break is at minute x, it occupies minute x; then the next break could be at minute x+1? That would have 0 minutes between them (since end of first at x:59? Actually we think in discrete minutes; they take whole minute). So there is no time between; they are adjacent. So that would violate if d>1. So indeed the start times must be at least d apart if d>1? Let's test: d=3, breaks at 1 and 5. The difference is 4 minutes; between end of first at minute 1 and start of second at minute 5, there are minutes 2,3,4 -> 3 minutes? Actually from minute 1 end to minute 5 start, there are minutes 2,3,4,5? Wait, break at minute 1 occupies minute 1; break at minute 5 occupies minute 5. The minutes in between are 2,3,4. That's 3 minutes. So that matches d=3. So start times difference =4, which is d+1? Let's compute: if break lasts 1 minute, to have exactly d minutes between breaks, the start times must differ by at least d+1? Because if break at minute x occupies [x, x], the next at minute y, with y > x, the minutes between are (x+1 ... y-1). The count is y - x - 1. Requirement: y - x - 1 >= d => y - x >= d+1. But the problem says "at least d minutes pass between any two coffee breaks". That could be interpreted as the number of minutes that pass between the two breaks, i.e., the gap. If break lasts 1 minute, the gap is the time from the end of one to the start of the next. So yes, y - x - 1 >= d => y - x >= d+1. However, many problems (like Codeforces 1042C? Actually I recall a Codeforces problem: "Coffee Break" maybe CF 1041B? or similar) typically define the condition as the difference between the start times is at least d, not d+1. Let's check the examples: Example 1: n=4,m=5,d=3, a=[3,5,1,2]. They output days=3. Their assignment: day1: minutes 1 and 5; day2: minute 2; day3: minute 3. Let's check day1: minutes 1 and 5. Difference =4. If requirement is start difference >= d, then 4 >=3 ok. If requirement is gap >= d, then gap = minutes between = 3 (minutes 2,3,4) => 3>=3 ok. Both conditions hold because d=3, and difference =4 gives gap=3. If they used difference >= d, then they could also assign minutes 1 and 4? But 4 not in list. But check minute 2 and 3: difference=1 <3 would violate. So day2 and day3 separate them. So example consistent.
Now second example: d=1. If requirement is start difference >=1, then any distinct minutes are okay. So all could be on one day. But they used 2 days. If requirement is gap >=1, then gap = y-x-1 >=1 => y - x >= 2. So start times must be at least 2 apart. That would mean consecutive minutes like 1 and 2 would be too close because gap=0. In that case, indeed, you cannot have breaks on minutes 1 and 2 on the same day because between them there is 0 minutes. So for d=1, you need at least 2 minutes between breaks? That would be a gap of at least 1 minute, i.e., start difference >=2. Then having breaks at all minutes 1..10 would be impossible on one day because, e.g., 1 and 2 have gap 0 <1. So you would need to split them into two sets such that no two in the same set are consecutive? That's exactly a 2-coloring of the interval graph where edges between numbers that differ by 1? Actually condition: gap >=1 => difference >=2. So conflicts if |a_i - a_j| <=1? Wait, if y - x >= 2, then the condition for allowed same day is |a_i - a_j| >= 2? Actually if start difference >=2, then it's allowed. So conflict if |a_i - a_j| < 2, i.e., difference <=1. So edges for difference <=1 (adjacent or equal). Since all minutes distinct, edges for difference =1. So that graph is essentially the graph where adjacent numbers are connected. The maximum clique? In such a graph, the maximum set where all pairwise are within distance 1 is just two consecutive numbers? Actually if you have three numbers like 1,2,3: pairwise: 1-2 diff=1 conflict, 2-3 diff=1 conflict, 1-3 diff=2 no conflict? Wait, 1 and 3 difference=2, which is not <2? For conflict we need difference <2? Actually if conflict when distance < required start difference? Let's define: we require start difference >= d+1? Or just >= d? Need to infer from example.
Let's test if the example assignment for d=1 uses rule start diff >= d+1? In output: they assign days: For minutes (in order of input? Input order: 10 5 7 4 6 3 2 1 9 8. Output days: 2 1 1 2 2 1 2 1 1 2. Let's list each minute with assigned day:
Minute:10 -> day2
5 ->1
7->1
4->2
6->2
3->1
2->2
1->1
9->1
8->2
So day1 has minutes: 5,7,3,1,9. Sort: 1,3,5,7,9. Day2: 10,4,6,2,8 -> sorted: 2,4,6,8,10. In each day, all minutes are odd or even respectively. Check pairwise differences: In day1, consecutive odds differ by at least 2. So start diff >=2. In day2, evens differ by at least 2. So indeed they split by parity. So condition seems to be that breaks on same day must have at least 2 minutes between them? That is start diff >=2. Since d=1, that would be start diff >= d+1.
But wait, the problem statement: "every day at least d minutes pass between any two coffee breaks." If a break lasts one minute, then the time between them is the minutes that are not part of either break. If you have breaks at minutes 1 and 2, then the minutes between them are none (since after minute 1 ends, minute 2 starts immediately). So 0 minutes pass between them, which is not at least 1. So indeed, the gap must be at least d minutes. So that matches: gap = (difference in start times) - 1 >= d => start difference >= d+1. So the condition is stricter: not just start difference >= d, but >= d+1. The example 1: d=3, they used 1 and 5: difference=4, gap=3, okay. If they had used 1 and 4: difference=3, gap=2, not okay because need gap >=3. So that would violate. So indeed, condition is gap >= d. That means for integer minute start times, the condition is |a_i - a_j| >= d+1 for i != j in same day. Or equivalently, if |a_i - a_j| <= d, then they cannot be on same day? Let's check: If difference = d, then gap = d-1, which is < d, so invalid. So conflict if |a_i - a_j| <= d. Because if difference <= d, gap <= d-1, which is less than d. So the conflict condition is: two breaks cannot be on same day if their minute difference is <= d. Because then the number of minutes between them would be less than d. Indeed, for d=3, difference=3 yields gap=2 <3; difference=2 yields gap=1; difference=1 yields gap=0; all invalid. So conflict threshold is d. So edges exist if |a_i - a_j| <= d.
In the first example, d=3, so conflict if difference <=3. In sorted list: 1,2,3,5. Conflicts: 1-2 diff=1 <=3 -> conflict; 1-3 diff=2 <=3 -> conflict; 1-5 diff=4 >3 -> ok; 2-3 diff=1 <=3 -> conflict; 2-5 diff=3 <=3 -> conflict; 3-5 diff=2 <=3 -> conflict. So graph is dense; max clique size? Points 1,2,3 all pairwise diff <=2 <=3, so clique of size 3. That matches.
For second example, d=1, conflict if difference <=1, i.e., adjacent minutes (since distinct, difference=1). So graph is a path on numbers 1-10 (edges between consecutive). Maximum clique size? In such a graph, the maximum set where all pairwise are adjacent? Actually clique means all pairs have an edge, i.e., all pairwise diff <=1. In a set of integers, this means all numbers are within distance 1 of each other. That can only be two numbers that are consecutive (like 5 and 6) but 5 and 6 diff=1 ok, but then 5 and 7 diff=2 not <=1, so not all pairwise. So maximum clique size in this graph is 2 (any two consecutive numbers). So chromatic number = 2. Indeed they used 2 days. So that matches the conflict condition of edges if diff <= d.
Thus we must adopt that: two breaks cannot be on the same day if the absolute difference in their minutes is <= d. So the condition is that for same day, |a_i - a_j| >= d+1. But it's easier to consider conflicts: if difference <= d, conflict. So we need to color the graph where vertices are points and edges between points with |a_i - a_j| <= d. Minimum number of colors equals the size of the maximum clique. The clique condition: a set of points such that any two have difference <= d. That's equivalent to all points fitting in an interval of length d. Because if max - min <= d, then any two diff <= d. Conversely, if all pairwise diff <= d, then max-min <= d. So max clique size is the maximum number of points in any interval of length d (inclusive of endpoints). Because if points are within an interval of length d, the maximum difference is <= d. For integer points, interval length d means max - min <= d. So we need to compute the maximum number of points that lie in any sliding window of size d (i.e., where a[right] - a[left] <= d). That's the min days = k.
Now we also need to assign colors (days) to points such that any two points with difference <= d get different colors, using exactly k colors (since k = chromatic number). The graph is a unit interval graph (with intervals of length d). Actually, each point can be represented as interval [a_i, a_i + d] (or [a_i, a_i+d])? Let's see: if we define interval I_i = [a_i, a_i + d], then two intervals overlap if they intersect: there exists x such that x in both, i.e., a_i <= x <= a_i+d and a_j <= x <= a_j+d => the condition is that a_i <= a_j+d and a_j <= a_i+d => |a_i - a_j| <= d. So edges correspond to overlapping intervals. Since intervals all have same length d, the graph is a proper interval graph (unit interval graph). That is a subclass of perfect graphs and is known to be "interval graphs" that are also claw-free? But important: it can be colored optimally by a greedy algorithm: sort intervals by start (or by a_i) and assign each vertex the smallest color not used by previously assigned intervals that overlap it. However, due to the special structure, there's a simpler method: we can assign colors by taking the sorted list and assigning each point's color = (index mod k) + 1? Let's test: For d=3, sorted: 1,2,3,5. k=3. index mod 3: 0->1,1->2,2->3,3->1 (since 3 mod 3=0?). Actually 0-indexed: 0->1,1->2,2->3,3->1. That gave days 1,2,3,1. Check conflicts: 1 and 2 have diff 1 <=3, they are colors 1 and 2 (different) ok. 1 and 3 diff 2 <=3: colors 1 and 3 different. 2 and 3 diff1 <=3: colors 2,3 diff. 1 and 5 diff4 >3 no conflict (same color allowed). So works. For d=1, sorted: 1..10, k=2. index mod 2: 1->1,2->2,3->1,4->2,... gives odds->1, evens->2? Actually 1-index: index 1->1,2->2,3->1,4->2,... That gives colors: 1,2,1,2,... which is exactly parity. That works because no two same parity are within diff <=1? Wait, parity: odds: 1,3,5,7,9. Check 1 and 3 diff=2 >1, so okay; 1 and 5 diff=4 >1; etc. So they are all separated by at least 2. Evens similar. So assignment by order modulo k works? Let's test other scenarios. Consider points: d=2, points: 1,2,3,4. Conflicts if diff<=2. Sorted: 1,2,3,4. What's max window size? Window length d=2: we need a[right]-a[left] <=2. Starting from 1: points 1,2,3? 3-1=2 <=2 => window size 3. 4-1=3 >2. 4-2=2 => window size 3 (2,3,4). So max clique size =3. k=3. Index mod 3: 1->1,2->2,3->3,4->1. So assignment: day1:1,4; day2:2; day3:3. Check conflicts: 1 and 4 diff=3 >2, so same day okay. 1 and 2 diff=1 <=2 => different days (1 vs2) ok. 2 and 3 diff=1 <=2 => days 2 vs3 ok. 3 and 4 diff=1 <=2 => days3 vs1 ok. Works. Another test: d=2, points: 1,2,4. Conflicts: 1-2 diff=1<=2 conflict, 1-4 diff=3>2 ok, 2-4 diff=2<=2 conflict. So graph is a triangle? Actually 1-2 edge, 2-4 edge, 1-4 not edge. That's not a clique; max clique size? The largest set with all pairwise diff<=2: {1,2} diff1<=2 ok; {2,4} diff2<=2 ok; {1,4} diff3>2 so not all; so max clique size 2. But can we color with 2? Let's try: 1 and 2 must differ, 2 and 4 must differ, 1 and 4 can be same. So two colors possible: assign 1->1,2->2,4->1 works. So k=2. Sorted: 1,2,4. Index mod 2: 1->1,2->2,3->1 (since third index 1-indexed gives 1) => days:1,2,1. Check: 1 and 2 diff ok (different), 2 and 4 diff=2<=2 but days 2 and 1 different, ok. Works. So modulo assignment seems to work for these tests.
But is modulo assignment always valid for unit interval graphs? I recall a known result: For an interval graph formed by intervals of equal length, the greedy coloring that processes intervals in order of their left endpoints and assigns the smallest available color yields an optimal coloring (with number of colors equal to max clique). Moreover, there is a known fact that if you sort the intervals by their left endpoints and assign each interval the color (i mod k) where k is the max clique size, that may produce a valid coloring? I'm not entirely sure if it's always valid. Let's reason.
We have points a_1 <= a_2 <= ... <= a_n (sorted). We have computed k = maximum number of points in any interval of length d. i.e., for any i, consider the set of points j such that a_j - a_i <= d; the maximum cardinality of such a set equals k. Now, we want to assign each point a color from 1..k such that if a_j - a_i <= d, then colors differ. One known method: assign color = (i mod k) + 1 (with 0-indexed i). But we need to ensure that if two points are within distance d, their indices differ by less than k? Actually, if a_j - a_i <= d, then they must be in the same sliding window of length d. By the pigeonhole principle, the maximum number of points in such a window is k. Therefore, within any set of points that are all within distance d (i.e., any clique), there are at most k points. However, two points that conflict might not be in the same window of exactly size d? Actually, if a_j - a_i <= d, then consider the window starting at a_i: all points from i to some j? But the crucial property: If we process points in sorted order, then for any point i, the set of points that conflict with i are those j > i such that a_j - a_i <= d. That set has size at most k-1? Actually the maximum number of points in [a_i, a_i+d] is at most k. That includes point i itself. So there are at most k-1 other points in that interval. So among indices greater than i, the number of points with a_j <= a_i+d is at most k-1. So if we assign colors in order, we could assign to point i the smallest color not used by previous conflicting points. Since previous points are those with index < i and conflict with i? Actually conflict can be with previous points if a_i - a_prev <= d. So if we process in increasing order, when we get to point i, we know which colors are forbidden by previous points that are within distance d. The number of such previous points is at most k-1 (because all points in [a_i-d, a_i] are within d of i, and that set includes i, so at most k points total, so at most k-1 previous). So there is always at least one color available if we have k colors. So greedy works.
Now the modulo assignment: color = i mod k (0-index) + 1. Is it equivalent to some greedy? Not necessarily, but maybe due to the structure it works. Let's test a potential counterexample. Suppose d=5, points: 1,2,6,7. Sorted: 1,2,6,7. Compute max window length d=5: intervals of length 5. Check: 1,2,6: 6-1=5 <=5 => window size 3. 2,6,7: 7-2=5 <=5 => window size 3. So k=3. Index mod 3 (0-index): indices 0->1, 1->2, 2->3, 3->1. So colors: 1->1, 2->2, 6->3, 7->1. Check conflicts: Conflict if diff <=5. 1 and 2 diff=1 <=5 => colors 1 vs2 ok. 1 and 6 diff=5 <=5 => colors 1 vs3 ok. 1 and 7 diff=6 >5 no conflict (but same color 1 and 1? 1 and 7 diff=6 >5, so allowed same color). 2 and 6 diff=4 <=5 => colors 2 vs3 ok. 2 and 7 diff=5 <=5 => colors 2 vs1 ok. 6 and 7 diff=1 <=5 => colors 3 vs1 ok. So valid.
Try a more complex set: d=3, points: 1,2,3,4,5. Sorted: 1..5. Max window length d=3: window of size 3? Actually interval length 3: points 1,2,3,4? 4-1=3 <=3 => 4 points? Wait 1,2,3,4: max-min=3 <=3, so 4 points can be in interval length 3? Let's check: 1 and 4 difference=3, which is <=3, so yes, 1,2,3,4 all pairwise diff <=3. So clique size 4. Check 1,2,3,4,5: 5-1=4>3. So max clique is 4? But also check 2,3,4,5: 5-2=3 <=3, so also 4. So k=4. n=5. Index mod 4 (0-index): 0->1,1->2,2->3,3->4,4->1. So colors: 1->1,2->2,3->3,4->4,5->1. Check conflicts: Need to ensure that any two points with diff <=3 have different colors. Let's list all pairs with diff <=3:
Pairs:
(1,2): diff1 <=3, colors 1,2 ok.
(1,3): diff2, 1 vs3 ok.
(1,4): diff3, 1 vs4 ok.
(1,5): diff4>3, same color 1 allowed? Actually 1 and 5 diff=4>3, so allowed same color. But both are color1; that's okay because condition is only if diff<=3 they must be different. So okay.
(2,3): diff1, 2 vs3 ok.
(2,4): diff2, 2 vs4 ok.
(2,5): diff3, 2 vs1 ok? 2 color2, 5 color1 -> different, ok.
(3,4): diff1, 3 vs4 ok.
(3,5): diff2, 3 vs1 ok.
(4,5): diff1, 4 vs1 ok.
All good. So works.
What about a case where points are not uniformly spaced? d=4, points: 1,2,5,6,9. Sorted: 1,2,5,6,9. Max window length 4: find max points within interval length 4. Check: 1,2,5: 5-1=4 <=4 => window size 3 (1,2,5). 2,5,6: 6-2=4 <=4 => 3 points. 5,6,9: 9-5=4 <=4 => 3 points. Also 1,2,5,6? 6-1=5>4 so not all. So k=3. Index mod3: 0->1,1->2,2->3,3->1,4->2. Colors:1->1,2->2,5->3,6->1,9->2. Check conflicts: Conflict if diff <=4. Check pairs:
(1,2): diff1<=4 -> colors1,2 ok.
(1,5): diff4<=4 -> colors1,3 ok.
(1,6): diff5>4 -> allowed same? 1 and 6 colors 1 and 1? diff=5>4, so allowed same color (1,1) ok.
(1,9): diff8>4, allowed same? 1 and 9 colors 1 and 2? Actually 9 color2, so different anyway.
(2,5): diff3<=4 -> 2 vs3 ok.
(2,6): diff4<=4 -> 2 vs1 ok.
(2,9): diff7>4 ok.
(5,6): diff1<=4 -> 3 vs1 ok.
(5,9): diff4<=4 -> 3 vs2 ok.
(6,9): diff3<=4 -> 1 vs2 ok.
Works.
Seems promising. But is there a proof? The idea: The graph is an interval graph and is also a "proper interval graph" (no containment). For proper interval graphs, there is an ordering such that the greedy coloring with k colors yields a coloring where the color assigned to vertex i is ((i-1) mod k)+1 if we sort by left endpoints. Actually, I recall a known result: In an interval graph where intervals have the same length, an optimal coloring can be obtained by sorting intervals by their left endpoints and assigning colors in a round-robin fashion. Because the overlapping pattern is such that the first k intervals all overlap each other (forming a clique) and then each subsequent interval overlaps exactly the previous k-1 intervals? Let's think.
Given sorted points a_1 <= a_2 <= ... <= a_n. Define intervals I_i = [a_i, a_i+d]. Since they all have same length d, these intervals are "proper" in the sense that no interval contains another (because if a_i < a_j, then a_i+d < a_j+d? Actually if a_i < a_j, then the intervals are nested? The left endpoints differ, but both have same length, so they are shifted copies, so no containment: the one with smaller left also has smaller right. So it's a proper interval graph. For proper interval graphs, there exists an ordering called "canonical ordering" where the greedy coloring yields an optimal coloring and the colors assigned are in order 1,2,...,k,1,2,...,k,... if we sort by left endpoints and then right endpoints? I think it's true because the graph is "unit interval", which is equivalent to an indifference graph? Actually, the graph where vertices are points on a line and edges between points with distance <= d is called an "indifference graph". Indifference graphs are known to be unit interval graphs. And they have the property that they are "interval graphs that are also claw-free". But more importantly, they are "comparability graphs"? Not sure.
Let's attempt a rigorous justification: We have sorted points. Let k = max number of points in any interval of length d. Claim: For any i < j, if a_j - a_i <= d, then j - i < k? Actually, not exactly: Because the maximum size of any set within an interval of length d is k. However, that does not directly imply that between i and j there are at most k-1 points inclusive? For i and j such that a_j - a_i <= d, the set of points from i to j (inclusive) all lie within [a_i, a_j] which is length <= d. So the number of points in that set (j-i+1) is <= k. So j-i+1 <= k => j - i <= k-1. So if two points conflict, their indices differ by at most k-1. Conversely, if indices differ by at least k, then they are definitely not conflicting? Possibly not: Could two points with index difference >= k still have difference <= d? Suppose we have points: a_1, a_2, ... such that they are spaced widely at first then close later. For i and j with j - i >= k, it could still be that a_j - a_i <= d if the points in between are sparse? Actually, if indices differ by >= k, then there are at least k+1 points from i to j inclusive? Wait, j-i >= k means j-i+1 >= k+1. But we know that any set of points within an interval of length d has size at most k. So if a_j - a_i <= d, then the points from i to j are all in [a_i, a_j] which length <= d, so size (j-i+1) <= k. So j-i+1 <= k => j-i <= k-1. Thus, if j-i >= k, then it's impossible for a_j - a_i <= d. So indeed, conflict implies index difference < k. So two points conflict only if their indices differ by at most k-1.
Now, if we assign colors by (i mod k) (0-index), then two points with indices differing by a multiple of k (including 0) get the same color. But if they differ by exactly k, they get same color, but we just argued that if indices differ by >= k, they cannot conflict. So same color for indices differing by k is safe because they don't conflict. However, if indices differ by less than k, they might conflict, but then their colors are different? Not necessarily: If we use (i mod k), then two indices i and j with 0 < j-i < k will have different colors? Let's check: If we number 0-index, colors = i mod k (range 0..k-1). Then if j-i is not a multiple of k, then i mod k and j mod k are different? Not necessarily. For example, k=4, i=1 (mod1), j=5 (mod1) but j-i=4, which is >=k, so conflict not possible. For j-i < k, could it happen that i mod k = j mod k? That would require that j-i is a multiple of k, but since 0 < j-i < k, the only multiple is 0, not possible. So for j-i < k, i mod k != j mod k. Therefore, any two indices with difference < k have distinct colors under modulo assignment. Since conflict requires difference < k, they will get different colors. This is a perfect argument!
Thus, modulo assignment works, given we use 0-indexed sorted order and assign color = (index % k) + 1. This ensures that any two points that conflict (i.e., a_j - a_i <= d) have indices difference < k, hence different colors. And we have exactly k colors. Therefore, it's a valid optimal coloring. The only nuance: The sorted order must be the order of increasing a_i. Since a_i are distinct and within [1,m], sorting yields increasing sequence. So we can sort the array, compute k via sliding window, then assign each original index a color based on its position in the sorted order. But careful: The output must list the day for each break in the order of the input. So we need to map back. So we will sort but remember original indices.
Thus algorithm:
- Read n, m, d, and array a of length n.
- Since d can be up to m, but note that m is up to 1e9, but n up to 2e5. Use 64-bit for differences.
- Create vector of pairs (a_i, original_index).
- Sort by a_i.
- Compute k: two-pointer sliding window:
left = 0, maxSize = 0.
for right in 0..n-1:
while a[right] - a[left] > d: left++.
maxSize = max(maxSize, right - left + 1)
Then k = maxSize.
Actually note: condition for conflict is a_j - a_i <= d, so window condition for points to be all pairwise within d? We want max number of points with max-min <= d. That's exactly while a[right] - a[left] > d, left++. So window satisfies a[right]-a[left] <= d. So size = right-left+1. So maxSize is k.
- Then assign ans array of size n.
For i from 0 to n-1 (sorted order):
color = i % k // 0..k-1
day = color + 1
ans[original_index] = day.
- Print k (the minimum days) on first line.
- Print ans array in input order.
Check with examples:
Example1: n=4,m=5,d=3, a=[3,5,1,2]. Sorted: (1,2), (2,3), (3,0), (5,1) (assuming original indices 0..3). Compute sliding window:
right0: (1) left=0 size=1 max=1
right1: (2) diff 2-1=1<=3, left=0 size=2 max=2
right2: (3) diff 3-1=2<=3, size=3 max=3
right3: (5) diff 5-1=4>3 => left++ becomes1 (point2). diff 5-2=3<=3, size=3 (indices1,2,3) max remains3. So k=3.
Then assign: i=0 color0->day1 to idx2 (original index for a=1? Actually original index of (1) is the third input? Input order: a1=3 idx0, a2=5 idx1, a3=1 idx2, a4=2 idx3. So sorted: (1, idx2) i=0 -> day1 to ans[2]; (2, idx3) i=1 -> day2 to ans[3]; (3, idx0) i=2 -> day3 to ans[0]; (5, idx1) i=3 -> day1 (since 3%3=0) to ans[1]. So ans = [3,1,1,2]? Wait: ans[0]=3, ans[1]=1, ans[2]=1, ans[3]=2 -> "3 1 1 2". That matches example output.
Example2: n=10,m=10,d=1, a list: 10,5,7,4,6,3,2,1,9,8. Sorted with indices: (1,7), (2,6), (3,5), (4,3), (5,1), (6,4), (7,2), (8,9), (9,8), (10,0). Compute window with d=1: need diff <=1? Actually condition for conflict is diff <= d (1). So window while a[right]-a[left] > 1, left++. Let's compute max size: The sorted list is 1,2,3,4,5,6,7,8,9,10. For each right:
right0:1 left0 size1 max1.
right1:2 diff1 <=1 left0 size2 max2.
right2:3 diff2>1 => left becomes1 (point2) diff 3-2=1 <=1 size2 (positions1,2) max remains2? Actually at right2, we have points 2 and3? After left=1, window [1]? Wait recalc: Initially left=0, right=2: a[2]=3, a[0]=1 diff=2>1 => left=1. Now a[2]-a[1]=3-2=1<=1, window indices1..2 size2. So max=2.
right3:4 diff with a[left=1]=4-2=2>1 => left=2 (a=3), diff 4-3=1 <=1 size2. So max stays2.
Similarly for all, max=2. So k=2.
Assign modulo 2: i=0->day1 to idx7 (original index for 1) => ans[7]=1; i=1->day2 to idx6 => ans[6]=2; i=2->day1 to idx5 => ans[5]=1; i=3->day2 to idx3 => ans[3]=2; i=4->day1 to idx1 => ans[1]=1; i=5->day2 to idx4 => ans[4]=2; i=6->day1 to idx2 => ans[2]=1; i=7->day2 to idx9 => ans[9]=2; i=8->day1 to idx8 => ans[8]=1; i=9->day2 to idx0 => ans[0]=2.
So ans order: idx0->2, idx1->1, idx2->1, idx3->2, idx4->2, idx5->1, idx6->2, idx7->1, idx8->1, idx9->2. That yields: 2 1 1 2 2 1 2 1 1 2, matches example output. Good.
Thus the solution is straightforward.
Now we must confirm that the condition for conflict is indeed a_j - a_i <= d (not < d). In the problem statement: "at least d minutes pass between any two coffee breaks". As argued, if breaks last one minute, then the gap is difference in start times minus one. So gap >= d => start_diff - 1 >= d => start_diff >= d+1. So two breaks cannot be on same day if start_diff <= d. So conflict if |a_i - a_j| <= d. So our edge condition is correct as per examples.
But wait: The first example had d=3 and they assigned 1 and 5 (diff=4) which is >=4? Actually 4 >= d+1=4? Yes, so okay. And they assigned 2 and 3 on different days because diff=1 <=3 => conflict. So yes.
Thus the solution is to compute k as max number of points in any window of length d (max-min <= d). But careful: The window length is d (since difference <= d). For sliding window, we use condition a[right] - a[left] > d (to move left) to maintain window with diff <= d. So that's correct.
Now complexity: O(n log n) for sorting, O(n) for sliding window and assignment.
One more nuance: The day length m is given but not used except maybe to bound a_i within [1,m]. However, we should consider that the breaks must occur within the working day minutes (1..m). That's satisfied because a_i are given within that range. And days are independent, each day has the same minutes. So no further constraints.
But is there any possibility that the maximum clique size is not simply the maximum number of points in a window of length d? Since we defined conflict if diff <= d. A clique is a set where every pair has diff <= d. That's equivalent to all points within an interval of length d. So yes.
Now prove the modulo assignment works: Sorted indices 0..n-1. For any i<j, if a_j - a_i <= d, then j-i+1 <= k, because all points from i to j are within [a_i, a_j] length <= d, so count <= k => j-i <= k-1. So j-i < k. Hence (i mod k) != (j mod k) because if they were equal, then (j-i) would be a multiple of k, but 0 < j-i < k implies the only multiple is 0, contradiction. So colors differ. This holds for any conflict pair. Also for non-conflicting pairs, same color allowed. So assignment valid.
Thus we have optimal coloring with exactly k colors.
Edge Cases:
-
n=1: k=1 obviously. Modulo assignment gives day1.
-
d >= m: Then the window condition a[right] - a[left] > d might be always false if d >= max difference (which is at most m-1). Actually if d >= m, then any two points have difference <= m-1 <= d, so conflict for all pairs. Then the graph is complete. Then max clique size = n. So k = n. Then modulo assignment: indices 0..n-1 mod n gives each a distinct color. That's perfect.
-
d=0? But constraints say d >= 1. So not needed.
-
Points can be large, but we use 64-bit.
Now, note: The problem states "more than d minutes pass between the end of any working day and the start of the following working day." This ensures that we don't need to worry about conflicts across days (e.g., a break at minute m of one day and minute 1 of next day would have difference 1 across days, but since there is > d minutes between days, it's fine). So indeed only within same day constraints matter.
Thus the solution is straightforward.
But wait: The sliding window algorithm to compute k: we used condition a[right] - a[left] > d. However, is the window of length d inclusive? Suppose d=3, points at 1 and 4: diff=3 <=3, conflict? According to condition, conflict if diff <= d. So 1 and 4 conflict. So they cannot be same day. So a window of points where max-min <= d is the clique. So we compute max size of such window. That's correct.
Now we assign colors modulo k. But we must be careful about the indexing: The sorted order indices start from 0. So color = (i % k) + 1.
Now let's test a scenario where the maximum clique size is k but there might be some points that are not captured in any single window of length d? Actually any clique corresponds to some window, so the sliding window will catch it.
Now let's test a tricky case: points: 1, 3, 5, with d=2. Conflicts: diff <=2? 1-3 diff=2 <=2 conflict; 3-5 diff=2 conflict; 1-5 diff=4 >2 no conflict. So graph is a path of two edges. Max clique size? The largest set where all pairwise diff <=2: {1,3} size2; {3,5} size2; {1,5} not; so k=2. Sliding window: sorted 1,3,5. For right=0: size1. right=1: 3-1=2 <=2 size2 max=2. right=2: 5-3=2 <=2 window left=1 size2; 5-1=4>2 so left moves to1, size2. So k=2. Mod assignment: i=0->1, i=1->2, i=2->1. So colors: 1->1,3->2,5->1. Check conflicts: 1 and 3 different; 3 and 5 different; 1 and 5 same, allowed. Works.
Now test case where points are not uniformly spaced but still produce a large window: d=5, points: 1,2,6,7,11. Sorted: 1,2,6,7,11. Window of length5: at right=2 (6): diff 6-1=5 <=5 => size3; right=3 (7): diff 7-2=5 => left=1 size3; right=4 (11): diff 11-6=5 => left=2 size3; So k=3. Mod assignment: i=0->1,1->2,2->3,3->1,4->2. Colors: 1->1,2->2,6->3,7->1,11->2. Check conflicts: Conflicts if diff <=5. Check pairs: 1-2 diff1 => colors1,2 ok; 1-6 diff5 => colors1,3 ok; 1-7 diff6 >5 allowed (colors1 and1? 1 and7 both color1? Wait 7 color1, diff6>5, allowed same color, ok); 2-6 diff4 <=5 => colors2,3 ok; 2-7 diff5 => colors2,1 ok; 2-11 diff9>5 allowed (2 vs2? 11 color2, diff9>5, allowed same? both color2, diff>5 so ok); 6-7 diff1 => colors3,1 ok; 6-11 diff5 => colors3,2 ok; 7-11 diff4 <=5 => colors1,2 ok. Works.
Now test a scenario where the sorted points have gaps but the maximum window is at the end, with size k, but the modulo assignment might cause a conflict with earlier points? But due to index difference property, if two points conflict, indices difference < k, so they will have different colors. So safe.
Therefore, the algorithm is correct.
Now implement.
But one subtlety: The problem statement: "for the given coffee break that is going to be on minute a_i, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least d minutes pass between any two coffee breaks." It doesn't explicitly state that the breaks are instantaneous or last one minute? But they said "each coffee break lasts exactly one minute". So indeed it's as we considered.
Now let's consider if the breaks can be taken at the exact minute a_i only, but the day assignment can be any day. So we treat each break as a point.
Now the algorithm:
- Read n, m, d.
- Read array a of n integers.
- Create vector of pairs: (a[i], i).
- Sort by a[i].
- Compute k: sliding window.
int left = 0, k = 0;
for right in 0..n-1:
while (sorted[right].first - sorted[left].first > d) left++;
k = max(k, right - left + 1);
- Then ans array of size n (int).
for i in 0..n-1:
int color = i % k; // 0-index
ans[sorted[i].second] = color + 1;
- Print k and ans.
Now double-check: In the sliding window, we used condition > d to move left. This ensures that window satisfies <= d. So k is max size of such window. That's correct.
Potential pitfalls: Overflow? a_i up to 1e9, d up to 1e9, difference up to 1e9, within 32-bit signed? 1e9 difference fits in 32-bit signed (max 2e9). But to be safe use long long.
Now confirm with some random tests using brute force for small n to ensure modulo assignment always works. We'll reason logically already proven, but we could test mentally.
Proof of index difference property: Suppose i<j and a_j - a_i <= d. Then consider indices i, i+1, ..., j. All these points have a values between a_i and a_j, so they lie within an interval of length <= d. Thus the number of points in that set is (j-i+1). Since the maximum number of points in any interval of length d is k, we have j-i+1 <= k => j-i <= k-1. So j-i < k.
Now modulo: If we use colors based on i mod k, then colors are the same only when i ≡ j (mod k). That would require j-i is a multiple of k. Since j-i is positive and less than k, the only multiple of k in that range is 0, but j-i>0, so not possible. Thus colors differ. QED.
Thus solution is solid.
Now one more nuance: The window length is d, not d-1. So we used d. Good.
Now we should consider if the assignment might exceed the number of days but still minimal? Yes we output k.
Now let's double-check the example with d=0? Not allowed.
Now we should also check that the days are counted from 1. That's fine.
Now finalize.
But wait, there is a nuance: The problem says "Monocarp also wants to take these n coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days)." That means we should minimize the number of days used, which we achieve by using k = chromatic number. And we output that.
Now I recall that this problem is Codeforces Round #1042? Actually it's from Codeforces 1042C? Hmm. I've seen a problem "Coffee Break" from Codeforces Round #1041? There is a known solution: sort and then assign using modulo. So it's consistent.
Now I'll produce final answer.
But we should also consider if there is any scenario where the maximum clique size computed as the sliding window is not exactly the chromatic number? For unit interval graphs, chromatic number = max clique size, so yes.
Edge case: When d is large enough that all points can be on one day, k=1, modulo assignment gives all day1.
Now check the constraint: n <= m, so points are within [1,m] and distinct.
Now one more thing: Could there be an issue with the assignment because of the fact that breaks happen at specific minutes within a day, and the day length m might impose that the first break of the day cannot be too early? No, any minute within 1..m is allowed. So no.
Thus the solution is complete.
Now we implement in code.
Time complexity O(n log n).
Let's produce final solution in C++.
Implementation details:
-
Use vector<pair<long long, int>> or just int since m <= 1e9, but long long safe.
-
Sorting: by first.
-
For sliding window: left index as integer.
-
Output: first line k, second line ans separated by spaces.
We'll test with given examples.
Now we should also note: In the sliding window, we use condition > d, not >=, because we want window where a[right] - a[left] <= d. So while it's > d, we move left.
Now final code.
Let's code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, d;
cin >> n >> m >> d;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
// sliding window to compute k
int k = 0;
int left = 0;
for (int right = 0; right < n; ++right) {
while (a[right].first - a[left].first > d) {
left++;
}
k = max(k, right - left + 1);
}
vector ans(n);
for (int i = 0; i < n; ++i) {
int day = (i % k) + 1;
ans[a[i].second] = day;
}
cout << k << "\n";
for (int i = 0; i < n; ++i) {
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}
But note: we used int for a.first, which is within 1e9, safe. However, difference might be up to 1e9, still fits in int (2e9). However, to avoid overflow when subtracting, we can cast to long long if needed but int should be fine as long as we don't add. Actually a[right].first - a[left].first could be negative if we misorder, but we have sorted, so it's non-negative. Using int is safe for values up to 2e9, but 1e9 difference is within 32-bit signed range (max 2,147,483,647). So okay.
Now let's double-check a tricky case: n=200000, d=1e9, a_i up to 1e9, but differences may be up to 1e9, still fine.
Now some languages may need long long, but in C++ int is fine? In many judges int is 32-bit. So maximum a_i = 1e9, min = 1, diff = 1e9 - 1 = 999,999,999, still < 2^31-1. So safe.
Thus solution done.
Now we should also verify that the assignment uses exactly k days. Modulo assignment uses colors 1..k. Could it skip some color? For n >= k, all colors appear at least floor(n/k) times. Since k = max window size, it's possible that n < k? Actually n >= 1, but could k be > n? No, k <= n because max clique size cannot exceed n. So n>=k? Actually if n=3 and max clique size could be 3, then k=3, n=3, all colors appear exactly once. If max clique size is 2 and n=3, then k=2, modulo uses colors 1,2,1; both colors used. So always uses at most k colors, but could it use fewer? If k is computed as max window size, it's minimal number of colors needed. The modulo assignment might sometimes not use all k colors? For example, if n < k? That cannot happen because k <= n. But if n=5 and k=4, modulo uses colors 1,2,3,4,1 -> all colors used. If k=n, then colors 1..n all used. So it uses exactly k colors (since at least the first k points get distinct colors 1..k). Because for i=0..k-1, i%k = i, so colors 1..k assigned to first k points. So yes, we use exactly k colors (days). So the first line should be k, not something else.
Now confirm with examples: yes.
Thus solution is correct.
Now we must produce answer. I'll include explanation in final.
But before final, I want to think if there is any possibility that the condition "more than d minutes pass between the end of any working day and the start of the following working day" could affect the coloring? It says: "Take into account that more than d minutes pass between the end of any working day and the start of the following working day." This ensures that breaks taken at the end of one day and the beginning of the next are automatically spaced enough, so we don't need to consider cross-day constraints. This is typical for such problems.
Thus final answer.
Now we can write solution.
I'll now produce the final response with code and explanation. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, d;
cin >> n >> m >> d;
vector<pair<int, int>> a(n); // (minute, original index)
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
// Sliding window to find the maximum number of points in any interval of length d
int k = 0, left = 0;
for (int right = 0; right < n; ++right) {
while (a[right].first - a[left].first > d) {
++left;
}
k = max(k, right - left + 1);
}
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
int day = (i % k) + 1; // 1‑based day number
ans[a[i].second] = day;
}
cout << k << "\n";
for (int i = 0; i < n; ++i) {
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}