Array (two-point lookup)

Source: Internet
Author: User

Array time limit:2000/1000ms (java/others) problem Description:
Give an array of a[n], so that s[i] is larger than the number of a[i in array A. Find the array S.
Input:
Input contains multiple sets of test data, for each set of data, enter an integer n (1<=n<=10^5), followed by n integer a[i] (0<=a[i]<=10^9), guaranteed input a[i] are all different.
Output:
For each set of data, the output array s.
Sample Input:
53 4 5 1 21011 4 12 5 3 8 10 6 7 9
Sample Output:
2 1 0 4 31 8 0 7 9 4 2 6 5 3
Problem-Solving ideas: Simple two-point search, water too! Upper_bound (B,b+n,a[i])-B returns the subscript of the first element greater than a[i]. The time complexity of a binary lookup is O (log (n)), so the total time complexity is approximately o (Nlog (n)).
AC Code:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=1e5+5;4 intN,A[MAXN],B[MAXN],S[MAXN];5 intMain () {6      while(~SCANF ("%d",&N)) {7          for(intI=0; i<n;++i) {scanf ("%d", &a[i]); b[i]=a[i];}8Sort (b,b+N); //Sort First 9          for(intI=0; i<n;++i)Tens[i]=n-(Upper_bound (B,b+n,a[i])-B);//Two-point search One          for(intI=0; i<n;++i) Aprintf"%d%c", s[i],i==n-1?'\ n':' '); -     } -     return 0; the}

Array (two-point lookup)

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.