算术教室
- 关键思路:
- 方法一:将a数组排好序后,然后遍历b数组,利用二分查找的方法(logn),在a数组中查找x-b[i];(这个方法先略过,以后有时间补上,这里主要是熟悉STL操作);
- 方法二:利用set集合,将a数组输入到set集合中,然后在集合中查找x - b[i]
(1)测试数据
5 4 12
1 3 4 5 9
3 4 6 10
(2)代码(set)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// 因为数组数据挺大的,所以我用long long
set<ll> A;
ll n, m, x;
int main() {
cin >> n >> m >> x;
for (int i = 0; i < n; ++i) {
ll num;
cin >> num;
A.insert(num);
}
for (int i = 0; i < m; ++i) {
ll num;
cin >> num;
// 因为是set集合,所以不会出现重复的数字,那么有就是1,没有这个数字就是0
if (A.count(x - num)) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}