【hdu5522 】【水】【是否存在x+y=m】

【链接】

http://acm.hdu.edu.cn/showproblem.php?pid=5522

【题意】

  1. 有T(1000)组数据

  2. 给你n(100)个数,每个数的数值都在[0,1000]之间。

  3. 让你任意选出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");
	}
}

猜你喜欢

转载自blog.csdn.net/running_acmer/article/details/82114212
m y
今日推荐