fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO() {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14.  
  15. void solve(){
  16. int n,q;
  17. cin >> n;
  18.  
  19. vector<int>a(n);
  20. for(auto &x : a) cin >> x;
  21.  
  22. sort(a.begin(), a.end());
  23.  
  24. cin >> q;
  25.  
  26. while(q--){
  27. int x;
  28. cin >> x;
  29.  
  30. if(x < a[0]){
  31. cout << 0 << '\n';
  32. continue;
  33. }
  34. if(x >= a[n-1]){
  35. cout << n << '\n';
  36. continue;
  37. }
  38.  
  39. int l = 0, r = n-1, ans = 0, mid;
  40.  
  41. while(l<=r){
  42. mid = (l+r)/2;
  43. if(a[mid] > x){
  44. ans = mid;
  45. r = mid - 1;
  46. }
  47. else{
  48. l = mid + 1;
  49. }
  50. }
  51. cout << ans << '\n';
  52. }
  53. }
  54.  
  55.  
  56. signed main(){
  57. FastIO();
  58.  
  59. int t = 1;
  60. //cin >> t;
  61.  
  62. while (t--){
  63. solve();
  64. }
  65. return 0;
  66. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
3 10 8 6 11
4
1
10
3
11
stdout
0
4
1
5