E. Two Platforms
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn points on a plane. The ii-th point has coordinates (xi,yi)(xi,yi). You have two horizontal platforms, both of length kk. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same yy-coordinate) and have integer borders. If the left border of the platform is (x,y)(x,y) then the right border is (x+k,y)(x+k,y) and all points between borders (including borders) belong to the platform.
Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same yy-coordinate.
When you place both platforms on a plane, all points start falling down decreasing their yy-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost.
Your task is to find the maximum number of points you can save if you place both platforms optimally.
You have to answer tt independent test cases.
For better understanding, please read the Note section below to see a picture for the first test case.
Input
The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.
The first line of the test case contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105; 1≤k≤1091≤k≤109) — the number of points and the length of each platform, respectively. The second line of the test case contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤1091≤xi≤109), where xixi is xx-coordinate of the ii-th point. The third line of the input contains nn integers y1,y2,…,yny1,y2,…,yn (1≤yi≤1091≤yi≤109), where yiyi is yy-coordinate of the ii-th point. All points are distinct (there is no pair 1≤i<j≤n1≤i<j≤n such that xi=xjxi=xj and yi=yjyi=yj).
It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
Output
For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally.
Example
input
Copy
4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3
output
Copy
6 1 5 10
Note
The picture corresponding to the first test case of the example:
Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points (1,−1)(1,−1) and (2,−1)(2,−1) and the second one between points (4,3)(4,3) and (5,3)(5,3). Vectors represent how the points will fall down. As you can see, the only point we can't save is the point (3,7)(3,7) so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point (5,3)(5,3) doesn't fall at all because it is already on the platform.
=========================================================================
很显然这两个区间是不能相交的,那么就暴力枚举左区间的终点,这个用前缀和即可,那么右区间就用后缀和可以达到。
# include<bits/stdc++.h>
using namespace std;
# define mod 998244353
typedef long long int ll;
struct node
{
int cnt;
ll pos;
};
struct node s[200000+10];
int a[200000+10];
ll pre[200000+10],bac[200000+10];
int main ()
{
//10 3 5 2 31 10 9 8 11 17 21 5
int t;
cin>>t;
int fuck=0;
while(t--)
{
fuck++;
ll n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
}
sort(a+1,a+1+n);
int now=0;
a[n+1]=a[n]+1;
int len=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i+1])
{
now++;
len++;
s[len].cnt=now;
s[len].pos=a[i];
now=0;
}
else
{
now++;
}
}
int l=1;
now=0;
for(int i=1;i<=len;i++)
{
now+=s[i].cnt;
while(l<=i&&s[l].pos+k<s[i].pos)
{
now-=s[l].cnt;
l++;
}
pre[i]=now;
}
int r=len;
now=0;
for(int i=len;i>=1;i--)
{
now+=s[i].cnt;
while(r>=i&&s[r].pos-k >s[i].pos)
{
now-=s[r].cnt;
r--;
}
bac[i]=now;
}
ll ans=0;
for(int i=1;i<=len;i++)
{
pre[i]=max(pre[i-1],pre[i]);
ans=max(ans,pre[i]);
}
bac[len+1]=0;
for(int i=len;i>=0;i--)
{
bac[i]=max(bac[i+1],bac[i]);
ans=max(ans,bac[i]);
}
for(int i=0;i<=len;i++)
{
ans=max(ans,pre[i]+bac[i+1]);
}
if(s[len].pos-s[1].pos<=2*k)
{
ans=n;
}
bac[0]=0;
cout<<ans<<endl;
}
return 0;
}