1001 number pairs in array and equal to K
Base time limit: 1 seconds Space limit: 131072 KB gives an integer k and an unordered array a,a the elements are n distinct integers, finding all and equal K pairs in array a. For example k = 8, Array a:{-1,6,5,3,4,2,9,0,8}, all and equal to 8 pairs include ( -1,9), (0,8), (2,6), (3,5). Input
Line 1th: 2 numbers separated by a space, K n,n is the length of a array. (2 <= n <= 50000,-10^9 <= K <= 10^9) Section 2-n + 1 rows: N elements of a array. ( -10^9 <= a[i] <= 10^9)
Output
Line 1-m: 2 numbers per line, requires a smaller number in front, and the m number is in ascending order of smaller numbers. If there is no one set of solutions output: no solution.
Input example
8 9-165342908
Output example
-1 90 82 63 5
//two-point optimization;#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;inta[50050];intv[50050];intN;intJudgeintnum) { intL=0, r=n-1; while(L <=r) {intMid= (l+r) >>1; if(num== A[mid])returnmid; if(num< A[mid]) r= mid-1; if(num> A[mid]) l=mid+1; } return-1;}intMain () {intK; while(SCANF ("%d%d", &k, &n)! =EOF) {memset (V,0,sizeof(v)); for(intI=0; i<n; i++) scanf ("%d", &A[i]); Sort (A, a+N); intF, S; BOOLFlag =0; for(intI=0; i< N; i++) { if(V[i])Continue; F=A[i]; S=k-A[i]; intindex=judge (s); if(Index! =-1&&!V[index]) { if(f==s)Continue; V[i]=1; V[index]=1; Flag=1; printf ("%d%d\n", F, s); } } if(!flag) printf ("No solution\n"); } return 0;}
1001 number pairs in array and equal to K