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)