ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021 ICPC新疆省赛 I - chino with mates ( 二分)向下取整与向上取整的坑

2021-10-10 10:32:32  阅读:263  来源: 互联网

标签:guest 109 characteristic ll ICPC mates 取整 female personality


题目链接:https://codeforces.com/group/yz4qgUJSxI/contest/103115/problem/I
一道简单的二分查找题
Chino held a blind date event, there are n male guests and m female
guests attending.

The personality characteristic value of each person can be represented
by an integer, the personality characteristic value of the i-th male
guest is ai , and the personality characteristic value of the j-th
female guest is bj

.

Chino believes that when the calculation result of the personality
characteristic values of two people ai×bj

in a certain range [l,r], the personality of two person is suitable each of them.

Chino would like to know how many matching combinations possible satisfied personality suitability between male and female guests.
Input

The first line contain two positive integers n,m(1≤n,m≤105)

The next line contain n integers ai(−109≤ai≤109)

indicates the personality characteristic value of the male guests.

Then the next line contain m integersbi(−109≤bi≤109)

indicates the personality characteristic value of the female guests.

the final line only contain two integers l,r(−109≤l≤r≤109)

Output

Output an integer on a line to indicate the number of matching combinations. Examples
Input

5 5
1 2 3 4 5
1 2 3 4 5
1 20

Output

24

Input

3 4
-1 -3 -5
-7 -6 -8 -9
9 9

Output

1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Note

For the first example, except that the male guest 5 and the female guest 5 do not meet the conditions, the other conditions are all
legal, so the answer is 25-1=24

For the second example, there is only a legal matching combination for male guest 1 and female guest 4.
这个题本来很简单的二分,L<=ai*bi<=R,两边同时除以一个bi,然后二分查找ai,显然是左边向上取整右边向下取整的范围内找,然后就出现了问题,我当时觉得直接用int型的R除以bi截断小数点就本来是向下取整,不用额外处理,结果。。负数的时候截断就成了向上取整。。

#include <bits/stdc++.h>
typedef long long ll;
const int maxn=100000+10;
using namespace std;
ll a[maxn+10],b[maxn+10];
int main()
{
    ll N,M;
    cin>>N>>M;
    for(int i=0;i<N;i++){
        scanf("%lld",&a[i]);
    }
    for(int j=0;j<M;j++){
        scanf("%lld",&b[j]);
    }
    sort(a,a+N);
    sort(b,b+M);
    ll L,R;
    cin>>L>>R;
    ll ans=0;
    for(int i=0;i<N;i++){
        if(a[i]==0){
            if(L<=0&&0<=R){
                ans+=M;
            }
            continue;
        }
        ll l,r;
        if(a[i]>0){
            l=ceil((double)L/(double)a[i]),r=floor((double)R/a[i]);
        }
        if(a[i]<0){
            l=ceil((double)R/(double)a[i]),r=floor((double)L/a[i]);
        }
        ll left=lower_bound(b,b+M,l)-b;
        ll right=upper_bound(b,b+M,r)-b;
        ans+=right-left;
    }
    cout<<ans<<endl;
    return 0;
}

标签:guest,109,characteristic,ll,ICPC,mates,取整,female,personality
来源: https://blog.csdn.net/tongjingqi_/article/details/120683265

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有