标签:
////二分搜索 时间复杂度为O(log n)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<math.h> #include<algorithm> using namespace std; #define N 200100 #define INF 0x3f3f3f3f int a[N], b[N]; int main() { int n, m; scanf("%d%d", &n, &m); for(int i=0; i<n; i++) scanf("%d", &a[i]); for(int i=0; i<m; i++) scanf("%d", &b[i]); sort(a, a+n); for(int i=0; i<m; i++) { int left, right, mid=-1; left=0, right=n-1; while(left<=right) { mid=(left+right)/2; if(a[mid]>b[i]) { if(a[mid-1] <= b[i]) break; right=mid-1; } else if(a[mid]<=b[i]) { if(mid == n-1) { mid ++;break; } left=mid+1; } } printf("%d%c", mid, i==m-1 ? ‘\n‘ : ‘ ‘); } return 0; }
标签:
原文地址:http://www.cnblogs.com/9968jie/p/5456722.html