Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will be possible to survive. But each vault can accommodate only k people and each person can pass only one unit of distance per one unit of time. Fortunately, all people and vaults are now on the straight line, so there is no confusion and calculations should be simple.
You are given the positions of the people and the vaults on the line. You are to find the maximal number of people who can hide in vaults and think they will survive.
Input
The first line contains four integers n, m, k and t (1 ≤ n, m, k ≤ 200000, 1 ≤ t ≤ 109) separated by spaces — the number of people, the number of vaults, the capacity of one vault and the time left to the Doomsday.
The second line contains n integers separated by spaces — the coordinates of the people on the line.
The third line contains m integers separated by spaces — the coordinates of the vaults on the line.
All the coordinates are between - 109 and 109, inclusively.
Output
Output one integer — the maximal number of people who can hide in vaults and think they will survive.
Example
Input
2 2 1 5
45 55
40 60
Output
2
Input
2 2 1 5
45 54
40 60
Output
1
Input
2 2 2 5
45 35
40 60
Output
2
Input
3 3 1 5
40 45 45
45 50 50
Output
3
左区间小的优先考虑,左区间相同的,右区间小的优先考虑
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int K = 2e5 + 10;
int b[K],c[K];
struct node{
int l,r;
}st[K];
bool cmp(node i,node j){
if(i.l == j.l) return i.r < j.r;
return i.l < j.l;
}
int main()
{
int n,m,k,t,a;
scanf("%d %d %d %d",&n,&m,&k,&t);
for(int i = 1; i <= n; i++)
scanf("%d",&a),st[i].l = a - t,st[i].r = a + t;
for(int i = 1; i <= m; i++)
scanf("%d",&b[i]);
int ans = 0;
sort(b + 1,b + 1 + m);
sort(st + 1,st + 1 + n,cmp);
for(int i = 1; i <= n; i++){
int ppl = lower_bound(b + 1,b + 1 + m,st[i].l) - b;
int ppr = upper_bound(b + 1,b + 1 + m,st[i].r) - b;
int ok = 1;
while(ppl < ppr && ok){
if(c[ppl] < k) ans++,c[ppl]++,ok = 0;
ppl++;
}
}
printf("%d\n",ans);
return 0;
}