老老实实写二分。
#include<cstdio> #include<algorithm> using namespace std; int n, a[1000001], x; int main() { scanf("%d", &n); for(int i=1; i<=n; ++i) scanf("%d", &a[i]); sort(a + 1, a + n + 1); while(scanf("%d",&x)!=EOF) { if (x > a[n]) {printf("%d ", n + 1); continue;} int left = 1, right = n, mid; while (left < right) { mid = (left + right) >> 1; if (x <= a[mid]) right = mid; else left = mid + 1; } printf("%d ", left); } return 0; }