0分求助

P1678 烦恼的高考志愿

kkkSb_O3 @ 2023-12-24 16:00:08

#include<bits/stdc++.h>
using namespace std;
long long int m,n,xx[1000005],xs[1000005],sum;
int f(int x)
{
    int l=1,r=m,mid;
    while(l<=r)
    {

        mid=(r+l)/2;
        if(xs[mid]==xx[mid])
        {
            return 0;
        }
        if(xx[mid]>xs[mid])
        {
            r=mid-1;
        }
        else if(xx[mid]<xs[mid])
        {
            l=mid+1;
        }
    }   
    return l;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    {
        cin>>xx[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin>>xs[i];
    }
    sort(xx+1,xx+1+m);
    sort(xs+1,xs+1+n);
    for(int i=1;i<=n;i++)
    {
        sum+=f(xs[i]);
    }
    cout<<sum;
}

by masonxiong @ 2024-02-07 17:00:20

@aungm 你f函数返回的是下标不是差值……?


|