二分
因为数组是单调的,所以我们可以用二分查找来做
# include <iostream>
# include <algorithm>
using namespace std;
int n,m,x;
int a[100010],b[100010];
int mid_find(int k)
{
int l = 0,r = m-1,mid =(l+r)/2;
while (l < r&&b[mid]!=k)
{
if (b[mid] > k) r = mid-1;
if (b[mid] < k) l = mid+1;
mid = (l+r)/2;
}
if (b[mid]==k) return mid;
return 0;
}
int main ()
{
cin >> n >> m >> x;
for (int i = 0 ; i < n ; i++) scanf("%d",&a[i]);
for (int i = 0 ; i < m ; i++) scanf("%d",&b[i]);
for (int i = 0 ; i < n ; i++)
{
if (mid_find(x-a[i]))
{
cout<<i<<' '<<mid_find(x-a[i]);
break;
}
else
continue;
}
return 0;
}
y总的双指针算法
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n, m, x;
int a[N], b[N];
int main()
{
scanf("%d%d%d", &n, &m, &x);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
for (int i = 0, j = m - 1; i < n; i ++ )
{
while (j >= 0 && a[i] + b[j] > x) j -- ;
if (j >= 0 && a[i] + b[j] == x) cout << i << ' ' << j << endl;
}
return 0;
}
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/40069/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。