AcWing800. 数组元素的目标和
给定两个升序排序的有序数组 A 和 B,以及一个目标值 x。
数组下标从 0 开始。
请你求出满足 A[i]+B[j]=x 的数对 (i,j)。
数据保证有唯一解。
输入格式
第一行包含三个整数 n,m,x分别表示 A 的长度,B 的长度以及目标值 x。
第二行包含 n 个整数,表示数组 A。
第三行包含 m 个整数,表示数组 B。
输出格式
共一行,包含两个整数 i 和 j。
数据范围
数组长度不超过 1e5。
同一数组内元素各不相同
1≤数组元素≤1e9
输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1
思路1:暴力算法-时间复杂度O(nm)
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int A[N], B[N], S[N];
int n, m, x, a, b;
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 ++)
{
for (int j = 0; j < m; j ++)
{
if ( A[i] + B[j] == x) cout << i << ' ' << j << endl;
}
}
return 0;
}
思路2:双指针算法-时间复杂度O(n + m)
#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;
}
数组单调递增
对于每个a[i]我们通过while循环判断是否a[i]+b[j]>x 后通过if判断是否当前的a[i] + b[j]为x
最终输出i与j
思路3:二分-时间复杂度O(nlogm)
#include <iostream>
using namespace std;
const int N = 1e5 + 5;
int n, m, x, a[N], b[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> x;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < m; i ++) cin >> b[i];
for (int i = 0; i < n; i ++) {
int l = 0, r = m - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (a[i] + b[mid] <= x) l = mid;
else r = mid - 1;
}
if (a[i] + b[l] == x) {
cout << i << ' ' << l << '\n';
break;
}
}
return 0;
}
思路4:利用单调性的二分做法-时间复杂度 O(n + logm)
找 j 花费的总时间为 logm + log(m/2) + log(m/4) + … + log2 ~ logm。加上 i ++ 次数,得 O(n + logm)
#include <iostream>
using namespace std;
const int N = 1e5 + 5;
int n, m, x, a[N], b[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> x;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < m; i ++) cin >> b[i];
// min a, max b, a + b = x, a+ -> b-
for (int i = 0, j = m - 1; i < n; i ++) {
int l = 0, r = j;
while (l < r) {
int mid = l + r + 1 >> 1;
if (a[i] + b[mid] <= x) l = mid;
else r = mid - 1;
}
j = l;
if (a[i] + b[j] == x) {
cout << i << ' ' << j << '\n';
break;
}
}
return 0;
}
思路5:双向二分-时间复杂度 O(logn + logm)
#include <iostream>
using namespace std;
const int N = 1e5 + 5;
int n, m, x, a[N], b[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> x;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < m; i ++) cin >> b[i];
// min a, max b, a + b = x, a+ -> b-
for (int i = 0, j = m - 1; i < n;) {
int l = 0, r = j;
while (l < r) {
int mid = l + r + 1 >> 1;
if (a[i] + b[mid] <= x) l = mid;
else r = mid - 1;
}
j = l;
if (a[i] + b[j] == x) {
cout << i << ' ' << j << '\n';
break;
}
l = i, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (a[mid] + b[j] >= x) r = mid;
else l = mid + 1;
}
i = l;
if (a[i] + b[j] == x) {
cout << i << ' ' << j << '\n';
break;
}
}
return 0;
}
其中暴力是最容易被理解的但是会被TLE,双指针的难度其实还行