B. Born This Way
memory limit per test256 megabytes
inputstandard input
outputstandard output
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<a2<…<an≤109) — the times the flights from A to B depart.
The third line contains m distinct integers in increasing order b1, b2, b3, …, bm (1≤b1<b2<…<bm≤109) — the times the flights from B to C depart.
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.
Examples
input
4 5 1 1 2
1 3 5 7
1 2 3 9 10
output
11
input
2 2 4 4 2
1 10
10 20
output
-1
input
4 3 2 3 1
1 999999998 999999999 1000000000
3 4 1000000000
output
1000000003
Note
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.
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.
思路:
总体思路是二分加暴力。
1:
剪切数大于等于任何一个航班数都,那么就可以把那个航班全部取消,就无法到达C地,
2:
然后看依次看从A到B的航班,从第1 的开始必有一个从B到C的最近航班,看取消数个,到达B地后看,在最近的航班是哪个。
然后看你从A到B的航班的哪个航班出发,下标是几(这里下标从0开始),也就是你先要取消几个航班。
然后看剩下的取消数加上从B到C的最近航班的下标看下标是不是大于等于从B到C的航班数量,也就是看从B到C的从最近航班向后取消后,还有没有航班,有就找到最大值,没有就算出 -1 。
操作:
1: 先判断剪切的数量大于等于任何一个航班的数量,那么就直接输出 -1,因为可以直接将通往某一地的航班取消那么,就永远无法到达C地。
2: 如果没有上面的情况,再看从A到B的航班依次暴力找到,在从B到C的航班中找到距离最近的航班的下标,然后算出从最近的航班可以向后再取消多少航班,算出取消后最近的航班的下标,如果大于等于取消从B到C的航班数量**(注意是下标大于等于)** ,那么就可以让他永远到不了C地,输出 -1 ,如果不是,就找到最大的那一个。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<sstream>
#define ll long long
#define mes(x,y) memset(x,y,sizeof(x))
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
ll i,j,x,n,m,ta,tb,cut;
while(cin>>n>>m>>ta>>tb>>cut){
vector<ll>a(n),b(m);
for(i=0;i<n;i++)cin>>a[i];
for(i=0;i<m;i++)cin>>b[i];
if(cut>=min(n,m)){
cout<<"-1"<<endl;continue;
}//先判断剪切的数量**大于等于**任何一个航班的数量,那么就直接输出-1,因为可以直接将通往某一地的航班取消那么,就永远无法到达C地。
ll maxn=-1,flag_a,lv=1;
for(i=0;i<=cut;i++){
flag_a=lower_bound(b.begin(),b.end(),a[i]+ta)-b.begin();//看从A到B的航班依次暴力找到,利用二分
flag_a+=cut-i;//在从B到C的航班中找到距离最近的航班的下标
if(flag_a>=m){
cout<<"-1"<<endl;lv=0;break;
}//如果**大于等于**取消从B到C的航班数量**(注意是下标大于等于)**,那么就可以让他永远到不了C地,输出-1
maxn=max(maxn,b[flag_a]+tb);//如果不是,就找到最大的那一个。
}
if(lv==1)cout<<maxn<<endl;
}
}