CF1148B.Born This Way

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C.

There are nn flights from A to B, they depart at time moments a1a_1, a2a_2, a3a_3, ..., ana_n and arrive at B tat_a moments later.

There are mm flights from B to C, they depart at time moments b1b_1, b2b_2, b3b_3, ..., bmb_m and arrive at C tbt_b moments later.

The connection time is negligible, so one can use the ii-th flight from A to B and the jj-th flight from B to C if and only if bj≥ai+tab_j \ge a_i + t_a.

You can cancel at most kk flights. If you cancel a flight, Arkady can not use it.

Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel kk flights. If you can cancel kk or less flights in such a way that it is not possible to reach C at all, print −1-1.

阿尔卡季购买了一张从城市 A 到城市 C 的机票。不幸的是,目前没有直飞航班,但存在大量从 A 到 B 的航班,以及从 B 到 C 的航班。

从 A 到 B 共有 nn 班航班,它们的起飞时刻分别为 a1a_1, a2a_2, a3a_3, ..., ana_n,并在起飞后 tat_a 时刻抵达 B。

从 B 到 C 共有 mm 班航班,它们的起飞时刻分别为 b1b_1, b2b_2, b3b_3, ..., bmb_m,并在起飞后 tbt_b 时刻抵达 C。

中转时间可忽略不计,因此第 ii 班从 A 到 B 的航班与第 jj 班从 B 到 C 的航班可以衔接当且仅当 bj≥ai+tab_j \ge a_i + t_a。

你最多可以取消 kk 班航班。一旦某班航班被取消,阿尔卡季便无法使用它。

阿尔卡季希望尽早抵达 C,而你则希望他尽可能晚地抵达 C。若你最优地取消 kk 班航班,求阿尔卡季最早可能抵达 C 的时刻。若你能通过取消至多 kk 班航班使得阿尔卡季完全无法抵达 C,则输出 −1-1。

输入格式

The first line contains five integers nn, mm, tat_a, tbt_b and kk (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5, 1≤k≤n+m1 \le k \le n + m, 1≤ta,tb≤1091 \le t_a, t_b \le 10^9) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively.

The second line contains nn distinct integers in increasing order a1a_1, a2a_2, a3a_3, ..., ana_n (1≤a1<a2<…<an≤1091 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 10^9) — the times the flights from A to B depart.

The third line contains mm distinct integers in increasing order b1b_1, b2b_2, b3b_3, ..., bmb_m (1≤b1<b2<…<bm≤1091 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 10^9) — the times the flights from B to C depart.

第一行包含五个整数 nn、mm、tat_a、tbt_b 和 kk(1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5,1≤k≤n+m1 \le k \le n + m,1≤ta,tb≤1091 \le t_a, t_b \le 10^9),分别表示从 A 到 B 的航班数量、从 B 到 C 的航班数量、A 到 B 的飞行时间、B 到 C 的飞行时间,以及你可以取消的航班数量。

第二行包含 nn 个严格递增的互异整数 a1a_1、a2a_2、a3a_3、…、ana_n(1≤a1<a2<…<an≤1091 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 10^9),表示从 A 到 B 的航班的出发时刻。

第三行包含 mm 个严格递增的互异整数 b1b_1、b2b_2、b3b_3、…、bmb_m(1≤b1<b2<…<bm≤1091 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 10^9),表示从 B 到 C 的航班的出发时刻。

输出格式

If you can cancel kk or less flights in such a way that it is not possible to reach C at all, print −1-1.

Otherwise print the earliest time Arkady can arrive at C if you cancel kk flights in such a way that maximizes this time.

如果可以通过取消 kk 次或更少的航班,使得完全无法到达城市 C,则输出 −1-1。

否则,在恰好取消 kk 次航班且使得 Arkady 到达城市 C 的时间尽可能晚的前提下,输出 Arkady 能够到达城市 C 的最早时间。

输入输出样例

  • 输入#1

    4 5 1 1 2
    1 3 5 7
    1 2 3 9 10

    输出#1

    11
  • 输入#2

    2 2 4 4 2
    1 10
    10 20

    输出#2

    -1
  • 输入#3

    4 3 2 3 1
    1 999999998 999999999 1000000000
    3 4 1000000000

    输出#3

    1000000003

说明/提示

Consider the first example. The flights from A to B depart at time moments 11, 33, 55, and 77 and arrive at B at time moments 22, 44, 66, 88, respectively. The flights from B to C depart at time moments 11, 22, 33, 99, and 1010 and arrive at C at time moments 22, 33, 44, 1010, 1111, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment 44, and take the last flight from B to C arriving at C at time moment 1111.

In the second example you can simply cancel all flights from A to B and you're done.

In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.

考虑第一个例子。从 A 到 B 的航班出发时刻分别为 11、33、55 和 77,到达 B 的时刻分别为 22、44、66、88。从 B 到 C 的航班出发时刻分别为 11、22、33、99 和 1010,到达 C 的时刻分别为 22、33、44、1010、1111。你最多可以取消两趟航班。最优方案是取消从 A 到 B 的第一趟航班以及从 B 到 C 的第四趟航班。这样 Arkady 需乘坐从 A 到 B 的第二趟航班,在时刻 44 抵达 B,并搭乘从 B 到 C 的最后一趟航班,在时刻 1111 抵达 C。

在第二个例子中,你只需取消所有从 A 到 B 的航班即可完成任务。

在第三个例子中,你只能取消一趟航班,最优方案是取消从 A 到 B 的第一趟航班。注意,此时仍有足够的时间赶上去往 C 的最后一趟从 B 出发的航班。

输入解题思路,AI测评打分。不知道怎么写?

首页