luogu#P16378. [NordicOI 2026] Alarms

    ID: 16499 远端评测题 7000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>二分2026双指针 two-pointerNordicOI(北欧)

[NordicOI 2026] Alarms

Problem Description

Your friend Emil has a large collection of alarms that go off at different times, to wake him up and remind him of various things. He has challenged you to choose some of his alarms and spend some time with them. You decide to choose the alarms in such a way that there is a period of time that is undisturbed that is as long as possible, so you can get some sleep.

Emil has NN alarms. The challenge is to choose KK of these alarms, and spend TT seconds with them. These TT seconds are the continuous time interval between t=0t=0 and t=Tt=T. The iith of Emil's alarms goes off at times t1,t2,,tmit_1, t_2, \dots, t_{m_i}, where mim_i is a positive integer. Your task is to find the maximum possible length of a time interval (L,R)(L,R), where 0LRT0 \leq L \leq R \leq T, such that none of the KK chosen alarms go off during that time. Note that the time interval is open (i.e., consists of all times tt such that L<t<RL < t < R). So it is OK if an alarm goes off at t=Lt = L or t=Rt = R.

Input Format

The first line contains three integers NN, KK, and TT (1KN31051 \leq K \leq N \leq 3 \cdot 10^5, 1T1091 \leq T \leq 10^9). These are the number of alarms Emil has, the number of alarms you should pick, and the amount of time the challenge takes place.

The following NN lines each contain a positive integer mim_i, followed by mim_i integers t1,t2,,tmit_1, t_2, \dots, t_{m_i} (1mi31051 \leq m_i \leq 3 \cdot 10^5, 0t1<t2<<tmiT0 \leq t_1 < t_2 < \dots < t_{m_i} \leq T). These are the times the iith alarm goes off.

The sum of all numbers mim_i will not exceed 31053 \cdot 10^5.

Output Format

Print one integer, the maximum length of a time interval where no alarms go off, if you choose KK alarms optimally.

3 2 5
1 1
1 4
2 2 3

3

2 2 7
8 0 1 2 3 4 5 6 7
8 0 1 2 3 4 5 6 7

1

3 2 100
1 10
3 0 2 3
4 3 5 6 8

92

Hint

Scoring

Your solution will be tested on a set of test groups. To earn points for a group, you must pass all test cases in that group.

Group Points Constraints
11 1818 K=NK = N
22 2727 mi=1m_i = 1 for all 1iN1 \leq i \leq N.
33 2020 The sum of all numbers mim_i is at most 300300.
44 3535 No additional constraints.

Explanation of Samples

In the first sample, the optimal strategy is to choose the first two alarms. That way, the time interval (1,4)(1,4) is undisturbed. This interval has length 33, and it is not possible to get a longer undisturbed time interval.

In the second sample, both alarms go off at every integer time and you have to pick both of them. In this case, the only undisturbed time intervals are in between consecutive integers. These all have length 11.

In the third sample, the challenge takes place during 100100 seconds, but all the alarms only go off during the first 1010 seconds. The optimal strategy is to pick the last two alarms, so that the interval (8,100)(8,100) is undisturbed.