fafu oj 1048 一个简单的问题
//fafu oj 1048 一个简单的问题 //二分#include#include #include using namespace std;const int N = 100005;int num[N];int n, p;bool find(int aim, int index){ int l = 0, r = n, mid = 0; while(l < r) { mid = (l + r) / 2; if(num[mid] == aim) { //若数据为 3 400 1 200 1111 所以要有以下判断 if(index != mid) return true; else if(num[index - 1] == aim || num[index + 1] == aim) return true; return false; } else if(num[mid] < aim) l = mid + 1; else r = mid - 1; } return false;}int main(){ freopen("in.txt", "r", stdin); while(scanf("%d%d", &n, &p) != EOF) { for(int i = 0; i < n; ++i) scanf("%d", &num[i]); sort(num, num + n); bool is_find = false; for(int i = 0; i < n; ++i) { if(num[i] > p) break; if( find(p - num[i], i) ) { is_find = true; break; } } if(is_find == true) puts("Yes"); else puts("No"); } return 0;}