所谓Two point ,是用两个指针,在两个数组中走,来求一些最大值
前提是两个数组升序(其实是有序)
我们用两个数组来求c,d的前缀最大值
然后运用Two point 来求最大值
代码如下
#include <cstdio>
#include <iostream>
using namespace std;
int a[9999],b[9999];
int max1[9999],max2[9999];
int main()
{
int n,p;
scanf("%d%d",&n,&p);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
for(int i=1,x;i<=n;i++)
{
scanf("%d",&x);
if(x>max1[i-1]) max1[i]=x;
else max1[i]=max1[i-1];
}
for(int i=1,x;i<=n;i++)
{
scanf("%d",&x);
if(x>max2[i-1]) max2[i]=x;
else max2[i]=max2[i-1];
}
int ans=0;
int t=n;
for(int i=1;i<=n;i++)
{
while(a[i]+b[t]>p&&t>=1) t--;
if(!t) break;
ans=max(ans,max1[i]+max2[t]);
}
printf("%d",ans);
return 0;
}