C++
$\color{#cc33ff}{— > 算法基础课题解}$
思路:
双指针
$\color{red}{双指针}:分析题目,看一下是否有单调性。如果有单调性,就可以把时间复杂度降低一位:$
$O(n * m) -> O(n + m)$
$时间复杂度:O(n + m)$
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, m, x;
int a[N], b[N];
int main() {
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, 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;
break;
}
}
return 0;
}