Advertisement
Josif_tepe

Untitled

Apr 6th, 2024
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9.     int k, n;
  10.     cin >> k >> n;
  11.    
  12.     int a[n], b[n];
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> a[i];
  15.     }
  16.     for(int i = 0; i < n; i++) {
  17.         cin >> b[i];
  18.     }
  19.    
  20.     sort(a, a + n);
  21.     sort(b, b + n);
  22.    
  23.     long long res = 0;
  24.     for(int i = 0; i < n; i++) {
  25.         if(a[i] >= k) {
  26.             res += n;
  27.         }
  28.         else {
  29.             int x = k - a[i];
  30.             int L = 0, R = n - 1;
  31.             int idx = -1;
  32.             while(L <= R) {
  33.                 int middle = (L + R) / 2;
  34.                 if(b[middle] < x) {
  35.                     L = middle + 1;
  36.                 }
  37.                 else {
  38.                     R = middle - 1;
  39.                     idx = middle;
  40.                 }
  41.             }
  42.             if(idx != -1 and b[idx] >= x) {
  43.                 res += n - idx;
  44.                
  45.             }
  46.         }
  47.     }
  48.     cout << res << endl;
  49.     return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement