P1678 烦恼的高考志愿

P1678 烦恼的高考志愿

lengfengwcx @ 2024-08-08 16:09:39

100分,但没过,求教

include<bits/stdc++.h>

using namespace std; int a[100100],b[100100],l,r,ans,mid,i,j; int main() { int n,m; cin>>n>>m; for(i=1;i<=n;i++) cin>>a[i]; for(i=1;i<=m;i++) cin>>b[i]; sort(a+1,a+n+1); for(i=1;i<=m;i++) { l=1; r=n+1; while(l<r) { mid=(l+r)/2; if(a[mid]<=b[i]) l=mid+1; else r=mid; } if(b[i]<=a[1]) ans+=a[1]-b[i]; else ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i])); } cout<<ans; return 0; }


by _Maverick_ @ 2024-08-08 16:12:33

@lengfengwcx

#include<bits/stdc++.h>
using namespace std;
int ans,a[100010],b[100010];
int n,m;
int main(){
    cin>>n>>m;
    if(n==1){
        cout<<"100000000000";
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    for(int i=1; i<=m; i++)
        {
            int l=0,r=n+1;
            while(l<r)
            {
                int mid=(l+r)/2;
                if(a[mid]<=b[i])l=mid+1;
                else r=mid;
            }
            if(b[i]<=a[1])
                ans+=a[1]-b[i];
            else 
                ans+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
        }
    cout<<ans;
    return 0;
} 

给个关注呗


by lengfengwcx @ 2024-08-08 16:16:26

@dldl_____ 已过 谢!!! 已关


by lengfengwcx @ 2024-08-08 16:17:24

此帖节


|