使用二分加暴力的方法可以解决这题,对第一个序列遍历,第二个序列进行二分查找。时间复杂度:n*log(2)n
源代码:
#include<stdio.h>
int a[50010], b[50010];
int main(){
int n1, n2;
scanf("%d", &n1);
for (int i = 0; i < n1; i++)scanf("%d", &a[i]);
scanf("%d", &n2);
for (int i = 0; i < n2; i++)scanf("%d", &b[i]);
int high=n2-1, low=0, mid=(high+low)/2;
if ((a[0] + b[n2 - 1]) > 10000 || (a[n1 - 1] + b[0]) < 10000)printf("NO\n");
else {
bool boo = false;
for (int i = 0; i < n1;i++){
while (high >= low){
mid = (low + high) / 2;
if ((a[i] + b[mid]) == 10000){
boo = true;
break;
}
if ((a[i] + b[mid]) > 10000)low = mid + 1;
else high = mid - 1;
}
high = n2 - 1; low = 0; mid = (high + low) / 2;
}
if (boo)printf("YES\n");
else printf("NO\n");
}
return 0;
}