Born This Way (二分)

题目传送门

题目描述

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 n flights from A to B, they depart at time moments a1, a2, a3, …, an and arrive at B ta moments later.

There are m flights from B to C, they depart at time moments b1, b2, b3, …, bm and arrive at C tb moments later.

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 bj≥ai+ta.

You can cancel at most k 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 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.

Input
The first line contains five integers n, m, ta, tb and k (1≤n,m≤2⋅105, 1≤k≤n+m, 1≤ta,tb≤109) — 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 n distinct integers in increasing order a1, a2, a3, …, an (1≤a1

The third line contains m distinct integers in increasing order b1, b2, b3, …, bm (1≤b1

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

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

样例1
输入

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

输出

11

样例2

输入

2 2 4 4 2
1 10
10 20

输出

-1

样例3

输入

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

输出

1000000003

题意:就是一个人要从A到C,但是A到C没有直接到达的飞机,需要从B地换乘,有n次航班从A到B,起飞时间分别是a1,a2,a2…an; 飞行时间是ta,有m次航班从B到C,起飞时间分别是b1,b2,b3,…bn; 飞行时间是tb,现在你们取消k次航班,让这个人尽可能的无法到达C,如果没办法无法到达,那么就让他尽可能晚的到达。

思路:这里我们用第一个例子来举例说明,
4 5 1 1 2
1 3 5 7
1 2 3 9 10

我们一共能取消2次航班,我们这里分三种情况

1 不取消A到B的航班,即从1时刻出发,所以在2时刻到达B,这是这个人能坐B机场的2 3 4 5次航班到达C,为了尽可能的让这个人晚点到C,我们取消B到C的第2第3趟航班,这样,这个人能乘坐第四趟航班,在10时刻到达C。

2 取消A到B的第1次航班,即从3时刻出发,所以在4时刻到达B,这是这个人能坐B机场的 4 5次航班到达C,为了尽可能的让这个人晚点到C,我们取消B到C的第4航班,这样,这个人能乘坐第5趟航班,在11时刻到达C。

3 取消A到B的第1第2次航班,即从5时刻出发,所以在6时刻到达B,这是没有机会再取消B到C的航班,所以这个人会乘坐B到C的第4次航班,在第10时刻到达C。

这就是以上所有的情况,所以最晚到达是11时刻。

AC代码

#include
using namespace std;
const int maxn=1e6+1000;
typedef long long ll;
const int inf=0x3f3f3f3f;//1-1e9
int a[maxn],b[maxn],n,m,k,ta,tb;
int main()
{
    cin>>n>>m>>ta>>tb>>k;
    for(int i=0;i<n;i++)
    cin>>a[i];
    for(int i=0;i<m;i++)
    cin>>b[i];
     if(k>=min(n,m))
     {
         cout<<-1<<endl;
         return 0;
     }
    int ans=0;
    for(int i=0;i<=k;i++)
    {
        int j=lower_bound(b,b+m,a[i]+ta)-b;
        if(k-i>=m-j)
        {
            cout<<-1<<endl;
            return 0;
        }
        ans=max(ans,b[j+k-i]+tb);
    }
    cout<<ans<<endl;
    //system("pause");
    return 0;
}

Born This Way (二分)_第1张图片

你可能感兴趣的:(二分)