#P14004. [eJOI 2025] Vacation

[eJOI 2025] Vacation

题目描述

Anton and his friends are planning a vacation together. They have already chosen the location; however, the dates are harder to agree on.

All NN friends have submitted in advance the days they plan to take off work. Friend ii originally scheduled their time off from day LiL_i to day RiR_i, inclusive. To maximize the time they can spend together, each friend may adjust their time off by shifting it earlier or later. Specifically, the ii-th friend can choose an integer did_i and move their time off to the interval [Li+di,Ri+di][L_i + d_i, R_i + d_i]. A positive did_i means taking time off later than originally planned, a negative did_i means earlier, and di=0d_i = 0 means keeping the original schedule.

The friends recognize that their bosses will not like the disruption caused by their changes. Therefore, they will only move their days off in a way such that the total movement of the intervals does not exceed some integer KK. Formally, they have to satisfy d0+d1++dN1K|d_0| + |d_1| + \cdots + |d_{N-1}| \leq K.

Help the friends figure out the maximum number of days all of them can be together if they change their schedules optimally.

Implementation details

You should implement the function plan_vacation:

int plan_vacation(int N, std::vector<int> L, std::vector<int> R, long long K)
  • NN: the number of friends
  • LL: a vector of NN positive integers, each of which denotes the first day off originally scheduled for that friend;
  • RR: a vector of NN positive integers, each of which denotes the last day off originally scheduled for that friend;
  • KK: the maximum allowed value of d0+d1++dN1|d_0| + |d_1| + \cdots + |d_{N-1}|.

This function will be called once for each test. It has to return the maximum number of days all friends can be together or 0 if that isn't possible at all.

输入格式

The input format is the following:

  • line 1: two integers – the values of NN and KK.
  • lines 2 to N+1N + 1: two integers – LiL_i and RiR_i.

输出格式

The output format is the following:

  • line 1: one integer – the return value of the call.
3 3
1 3
5 9
2 5
2

提示

Example

Consider the following call:

plan_vacation(3, {1, 5, 2}, {3, 9, 5}, 3)

The friends have requested the following intervals of days off: [1,3][1, 3], [5,9][5, 9], [2,5][2, 5]. Therefore, friend 0 can move their time off to 2 days later and friend 1 their time off to 1 day earlier to get [3,5][3, 5], [4,8][4, 8], [2,5][2, 5]. Then, all friends would be available on day 4 and day 5, which results in 2 days in common. It can be proven that they can't do better with K=3K = 3. Therefore, the function should return 2.

Constraints

  • 1N5000001 \leq N \leq 500\,000
  • 1LiRi1091 \leq L_i \leq R_i \leq 10^9
  • 0K10180 \leq K \leq 10^{18}

Subtasks

Subtask Points Required subtasks Additional constraints
0 - The example.
1 7 K=0K = 0
2 11 1 K1K \leq 1
3 6 - K=1018K = 10^{18}
4 13 0 N104N \leq 10^4, Li10L_i \leq 10, Ri10R_i \leq 10
5 18 N103N \leq 10^3
6 29 0, 4, 5 N105N \leq 10^5
7 16 0 - 6 -