I. chino with mates

古城微笑少年丶 2022-10-13 12:42 222阅读 0赞

题目大意

blind date event – 相亲

n n n 名男性, m m m 名女性

男性的个性值为 a i a_i ai​, 女性的个性值为 b j b_j bj​ ,当 a i ∗ b i 在 [ L , R ] a_i * b_i在 [L, R] ai​∗bi​在[L,R] 这个范围就算配对合格。

求有多少对?


题解

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

注意边界即可


代码

  1. #include <bits/stdc++.h>
  2. #define rep(i,l,r) for(int i=l;i<=r;i++)
  3. #define all(a) (a).begin(),(a).end()
  4. #define pb push_back
  5. using namespace std;
  6. typedef long long ll;
  7. typedef vector<int> vi;
  8. ll ans;
  9. int n,m,tmp,l,r;
  10. int main() {
  11. cin >> n >> m;
  12. vi a,b;
  13. rep(i,1,n) cin >> tmp, a.pb(tmp);
  14. rep(i,1,m) cin >> tmp, b.pb(tmp);
  15. cin >> l >> r;
  16. sort(all(a));
  17. sort(all(b));
  18. for (auto &x : a)
  19. {
  20. if (x < 0)
  21. ans += upper_bound(all(b), floor((double)l / x)) - lower_bound(all(b), ceil(double(r) / x));
  22. else if (x > 0)
  23. ans += upper_bound(all(b), floor((double)r / x)) - lower_bound(all(b), ceil(double(l) / x));
  24. else if (l <= 0 && r >= 0) ans += m;
  25. }
  26. cout << ans;
  27. return 0;
  28. }

发表评论

表情:
评论列表 (有 0 条评论,222人围观)

还没有评论,来说两句吧...

相关阅读

    相关 F. chino with ball

    题目大意 有 N N N 个小球在光滑水平的地上,初始速度有三种情况, − 1 , 0 , 1 -1, 0, 1 −1,0,1 求 k k k 秒后各个小球的位置