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.
Input example
8 9-165342908
Output example
-1 90 82 63 5
Two points can also be.
But monotonic optimizations can be directly complex to O (n)
1 /*************************************************************************2 > File name:code/51nod/1001.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 October 19 Monday 18:31 13 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> -#include <cctype> + A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineMS (A,X) memset (A,x,sizeof (a)) - using namespacestd; - Const intdx4[4]={1,0,0,-1}; - Const intdy4[4]={0,-1,1,0}; -typedefLong LongLL; intypedefDoubleDB; - Const intINF =0x3f3f3f3f; to Const intn=5e4+7; + intK,n; - intA[n]; the intMain () * { $ #ifndef Online_judgePanax NotoginsengFreopen ("In.txt","R", stdin); - #endif the +scanf"%d%d",&k,&n); A for(inti =0; I < n; i + +) scanf ("%d",&a[i]); theSort (a,a+n); + intj = N1; - BOOLFlag =false; $ for(inti =0; I < n1; i++) $ { - while(i<j&&a[i]+a[j]>k) j--; - if(a[i]+a[j]==k&&i<j) the { -Flag =true;Wuyi //cout<< "I:" <<i<< "J:" <<j<<endl; theprintf"%d%d\n", A[i],a[j]); - } Wu } - if(!flag) puts ("No Solution"); About $ - #ifndef Online_judge - fclose (stdin); - #endif A return 0; +}
View Code
51nod 1001 Pairs (monotonic optimization) and equal to K in the array