codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1148B.md (3267B)


      1 Source: https://codeforces.com/problemset/problem/1148/B
      2 
      3 B. Born This Way
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later.
     24 
     25 There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later.
     26 
     27 The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$.
     28 
     29 You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it.
     30 
     31 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 $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$.
     32 
     33 Input
     34 
     35 The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \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.
     36 
     37 The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 10^9$) — the times the flights from A to B depart.
     38 
     39 The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 \lt b_2 \lt \ldots \lt b_m \le 10^9$) — the times the flights from B to C depart.
     40 
     41 Output
     42 
     43 If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$.
     44 
     45 Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time.
     46 
     47 Examples
     48 
     49 Input
     50 
     51 ```
     52 4 5 1 1 2
     53 1 3 5 7
     54 1 2 3 9 10
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 11
     61 ```
     62 
     63 Input
     64 
     65 ```
     66 2 2 4 4 2
     67 1 10
     68 10 20
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 -1
     75 ```
     76 
     77 Input
     78 
     79 ```
     80 4 3 2 3 1
     81 1 999999998 999999999 1000000000
     82 3 4 1000000000
     83 ```
     84 
     85 Output
     86 
     87 ```
     88 1000000003
     89 ```
     90 
     91 Note
     92 
     93 Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, 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 $4$, and take the last flight from B to C arriving at C at time moment $11$.
     94 
     95 In the second example you can simply cancel all flights from A to B and you're done.
     96 
     97 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.