Test instructions
Given an integer k and an unordered array, the elements of a,a are n distinct integers, finding the pairs of all and equal k 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.
Ideas:
Sort, enumerate the first number, and use Lower_bound to find the second number.
Code:
intk,n;ll a[50000+Ten];intMain () {CIN>>k>>N; Rep (I,1, n) Scan ("%lld",&A[i]); Sort (a+1, A +1+N); A[n+1]=INF; BOOLFound=0; Rep (I,1, N-1) {ll tmp=k-A[i]; if(tmp<a[i+1] || Tmp>a[n])Continue; if(A[lower_bound (a+i+1, A +1+N,TMP)-a]==tmp) {Print ("%lld%lld\n", a[i],tmp); FOUND=1; } } if(! FOUND) puts ("No Solution"); RET0;}
51NOD_1001 number pairs in array and equal to K (two points)