【链接】
http://acm.hdu.edu.cn/showproblem.php?pid=5522
【题意】
-
有T(1000)组数据
-
给你n(100)个数,每个数的数值都在[0,1000]之间。
-
让你任意选出3个数A、B、C,问你是否有情况满足A=B+C。
【时间复杂度&优化】
O(n^3) or O(n2logn)
【代码】
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 7;
int a[maxn];
int n;
int f() {
set<int>st;
for (int i = n; i >= 1; i--) {
for (int j = 1; j < i; j++) {
if (st.find(a[i] + a[j]) != st.end())return 1;
}
st.insert(a[i]);
}
return 0;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + 1 + n);
puts(f() ? "YES" : "NO");
}
}