51NOD_1001 number pairs in array and equal to K (two points)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.