#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int FIND(int l,int r,int aim) //二分查找,从l到r,查找aim { int mid; while(l<=r) { mid=(l+r)/2; if(arr[mid]==aim) return mid; //找到:返回坐标 else if(arr[mid]<aim) l=mid+1; else r=mid-1; } return -1; //没有找到 } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) scanf("%d",&arr[i]); sort(arr,arr+n); //排序之后才能用二分查找 for(int i=0 ; i<n ; ++i) { int x=FIND(i+1,n-1,m-arr[i]); //找下标为i数字的对立数,l从i+1开始可以保证找到的数下标不是原数。而且数组升序,前面的没找到,后面的也用不到前面的了 if(x!=-1) //-1代表对立数字存在 //另一种写法:FIND(0,n-1,m-arr[i]) if(x!=-1 && x!=i) 还要求了存在的数字不是原来的arr[i] { printf("%d %d",arr[i],m-arr[i]); return 0; } } printf("No Solution"); return 0; }
时间: 2024-08-03 08:06:15