Base time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm problem
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
Tonight, the only one without a topic, because I and t change problem is also adjusted for a long time.
The AC code is attached:
1#include <iostream>2#include <algorithm>3#include <cstring>4 using namespacestd;5 6 Const intinf=1<< -;7 8 inta[50010];9 Ten intMain () { One intNLong LongK; ACin>>k>>N; - for(intI=0; i<n;i++){ -Cin>>A[i]; the } -Sort (a,a+n); - /*for (int i=0;i<n;i++) - cout<<a[i]<< "";*/ + intt=n-1, i=0, ans=0; - while(1){ + if(a[i]+a[t]==k&&i<t) { Acout<<a[i]<<" "<<a[t]<<Endl; atans++; -i++; - } - Else if(a[i]+a[t]>k) { -t--; - } in Else if(a[i]+a[t]<k) { -i++; to } + if(i>=t) - Break; the } * if(ans==0){ $cout<<"No Solution"<<Endl;Panax Notoginseng } - return 0; the}
Best of all:
1#include <stdio.h>2#include <algorithm>3 using namespacestd;4 5 Const intN =50005;6 7 intk, N;8 intA[n];9 Ten intMain () { One intI, J; Ascanf"%d%d", &k, &n); - for(i =0; I < n; i++) { -scanf"%d", &a[i]); the } -Sort (A, A +n); - BOOLFlag =false; -i =0; j = N-1; + while(I <j) { -n = a[i] +A[j]; + if(n = = k) {printf ("%d%d\n", a[i++], a[j--]); Flag =true; } A Else if(N < k) {i++; } at Else{j--; } - } - if(!flag) {Puts ("No Solution"); } -}
1001 number pairs in array and equal to K