#include<stdio.h>#include<algorithm>Usingnamespace Std;int arr[100066];int FIND (int L,int R,int aim)Binary lookup, from L to R, find aim{int mid;while (l<=r) {mid= (l+r)/2;if (Arr[mid]==aim)return mid;Find: Return coordinatesElseif (Arr[mid]<aim) l=mid+1;else r=mid-1; }Return-1;Not found}int main () {int n,m; scanf"%d%d", &n,&m);Forint i=0; I<n; ++i) scanf ("%d", &arr[i]); Sort (arr,arr+n);Sorting before you can find it with two pointsfor (int i=0; i<n; ++i) {int x=find (i+1,n-1,m-arr[i]); //find the opposite number labeled I number, L from the beginning of the i+1 can be guaranteed to find the number subscript is not the original number. And the array ascending, the front is not found, the back is not used in front of the if (x!=-1) //- 1 represents the existence of opposing numbers//another notation: FIND (0,n-1,m-arr[i]) if (x!=-1 && x!=i) also requires that the existing number is not the original arr[i] {printf (< Span class= "delimiter" > "%d%d" arr[i],m-arr[i]); return 0;} printf ( "no solution" ); return 0;}
pat:1048. Find Coins (25) (two-point lookup) AC