1001 pairs in array and equal to K (51nod)

Source: Internet
Author: User

1001 Number pair base time limit in array and equal to K: 1 seconds Space limit: 131072 KB Score: 5 Difficulty: 1-level algorithm topic collection focus on giving 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



Just began to write when thought is just a simple row and then look for the order to see if there are satisfied with the conditions,,, did not expect to pay a few hair, incredibly timed out,, Soga, the original time complexity to O (n^2) ...
In the search for the need to optimize, the following is attached to the timeout code and AC code, and then went to the Internet to find, incredibly used the Lower_bound this function,
Timeout code
#include <bits/stdc++.h>using namespacestd;intnum[50001];intMain () {intK,n; CIN>>k>>N;  for(intI=0; i<n;i++) Cin>>Num[i]; Sort (Num,num+N); intflag=0;  for(intI=0; i<n;i++)    {       for(intj=n-1; j>i;j--)      {          if(num[i]+num[j]==k) {cout<<num[i]<<" "<<num[j]<<Endl; Flag=1; }                           }             }    if(flag==0) cout<<"No Solution"<<Endl; return 0;}

AC Code

1#include <bits/stdc++.h>2 using namespacestd;3 intnum[50001];4 intMain ()5 {6     intK,n;7Cin>>k>>N;8      for(intI=0; i<n;i++)9Cin>>Num[i];TenSort (num,num+n); One     intflag=0; A   intI=0; -   intt=n-1; -   intsum; the    while(i<t) -   { -sum=num[i]+Num[t]; -       if(sum==k) +       { -printf"%d%d\n", num[i++],num[t--]); +flag=1; A       } at        -     Else if(sum<k) -i++; -     Else -t--; -  in   } -     if(flag==0) tocout<<"No Solution"<<Endl; +     return 0; -}

Lower_bound Solution

#include"iostream"#include<algorithm>#defineN 50006using namespacestd; Long LongNum[n]; intMain () {Long Longk, N; BOOLFlag =true; CIN>> k >>N;  for(inti =0; I < n; i++) {cin>>Num[i]; } sort (num, num+N);  for(inti =0; I < n; i++)      {          intpos = lower_bound (num, num + N, k-num[i])-num; if(Num[i] + num[pos] = = k && pos >i) {cout<< Num[i] <<" "<< Num[pos] <<Endl; Flag=false; }                }      if(flag) {cout<<"No Solution"<<Endl;      } getchar ();      GetChar (); return 0; }  

Take this opportunity to understand some of the uses of the Lower_bound function

This function is a function within the C + + STL

It is included in the header file #include <algorithm>

Precautions

An ordered sequence must be determined before calling Lower_bound, otherwise an error is called

The function Lower_bound () returns the first element position that is greater than or equal to Val in the front closed interval of first and last. If all elements are less than Val, the last position is returned

Examples are as follows:

An array number sequence is: 4,10,11,30,69,70,96,100. Set the subscript to insert the digit 3,9,111.pos to the position you want to insert

The

pos = lower_bound (number, number + 8, 3)-Number,pos = 0. That is, the position of the subscript of the number array is 0.

pos = lower_bound (number, number + 8, 9)-number, pos = 1, which is the position of the subscript 1 for the number array (that is, where 10 is located).

pos = lower_bound (number, number + 8, 111)-Number, pos = 8, which is the position of the number array with the Subscript 8 (but the subscript is capped at 7, so the next element of the last element is returned).

So, remember: the function Lower_bound () returns the first element position greater than or equal to Val in the front-closed interval of first and last in the binary lookup. If all elements are less than Val, then the last position is returned, and the last position is out of bounds!! ~

Returns the position of the first element of the lookup element, that is, the element value >= find value.




1001 pairs in array and equal to K (51nod)

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.