STL binary search: Hdu 5178 (bestcoder Round #31 1001)

Source: Internet
Author: User
Tags cmath

The STL contains four different binary search algorithms ,binary_search lower_bound upper_bound equal_range. Their scopes are already sorted a good array.

Binary_search tries to find the element value in the sorted [first, last]. Returns true if it is found, or false if it does not return a lookup location.
iterator lower_bound(const key_type &key): Returns an iterator that points to the key value >= key the first element. iterator upper_bound(const key_type &key): Returns an iterator that points to key values > Keys the first element. HDU 5178 Pairs

Problem Descriptionjohn has n Points on the X axis, and their coordinates is (x[I],0),(I=0,1,2,... ..,N?1) . He wants to know how many pairs <a,b> That |x[b]?x[a]|≤k.(a<b)
Inputthe first line contains a single integer T (about 5), indicating the number of cases.
Each test case begins with a integers N,k(1≤N≤100000,1≤k≤ ten 9 ) .
Next n Lines contain an integer x[I](? Ten 9 ≤x[I]≤ ten 9 ) , means the X coordinates.
Outputfor each case, output a integer means how many pairs <a,b> That |x[b]?x[a]|≤k .
Sample Input
25 5-10001001011025 300-1000100101102

Sample Output
310

Sourcebestcoder Round #31


Reference code one (using binary lookup function Upper_bound)


#include <iostream> #include <cstdio> #include <algorithm> #include <map> #include <vector > #include <queue> #include <cstring> #include <cmath> #include <climits> #define EPS 1e-10using namespace Std;typedef long long ll;const int inf=int_max;const int MAXN = 1e5+10;int N,k,a[maxn];int Main () {//  freopen ("Input.txt", "R", stdin);   int t;cin>>t;   while (t--) {    cin>>n>>k;    for (int i=0;i<n;i++) scanf ("%d", a+i);    Sort (a,a+n);    ll Ans=0;    for (int i=0;i<n;i++) {     ans+=upper_bound (a,a+n,a[i]+k)-a-1-i;//upper_bound return value-A gets the number of elements <=a[i]+k, Again-1 is to correct the initial position starting from 0, and the last minus I get is the pair number with I    }    printf ("%lld\n", ans);   }   return 0;}


Reference Code II (handwritten binary lookup function)



#include <iostream> #include <cstdio> #include <algorithm> #include <map> #include <vector > #include <queue> #include <cstring> #include <cmath> #include <climits> #define EPS 1e-10using namespace Std;typedef long long ll;const int inf=int_max;const int MAXN = 1e5+10;int N,k,a[maxn];ll Ans;int bin Ary_search (int num,int pos) {int low=pos+1,high=n-1,mid,res=0, while (Low<=high) {    mid= (low+high) >>1;    if (a[mid]-num>k) high=mid-1;    else{    res=mid-pos;//directly get the number of pair, or else to determine whether there is no result to deal with the    low=mid+1;}    }  Return res;//No then returns the initial value 0}int main () {  //Freopen ("Input.txt", "R", stdin);   int t;cin>>t;   while (t--) {    scanf ("%d%d", &n,&k);    for (int i=0;i<n;i++) scanf ("%d", a+i);    Sort (a,a+n);    ans=0;    for (int i=0;i<n;i++) {      ans+=binary_search (a[i],i);//cumulative Process    }    printf ("%lld\n", ans);   }   return 0;}



STL binary search: Hdu 5178 (bestcoder Round #31 1001)

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.