10分二分代码求指导

P1678 烦恼的高考志愿

hkr114514 @ 2024-08-07 09:23:11

#include <bits/stdc++.h>
using namespace std;
int mm, nn, m[100010], n[100010],ans=0;
int main()
{
    cin >> mm >> nn;
    for (int i = 1; i <= mm; i++)
    {
        cin >> m[i];
    }
    sort(m + 1, m + mm + 1);
    for (int i = 1; i <= mm; i++)
    {
        cin >> n[i];
        int L = 1, R = mm, mid ;
        while (L < R)
        {
            mid = L + R + 1 >> 1;
            if (m[mid] <= n[i])
            {
                L = mid;
            }
            else
            {
                R = mid - 1;
            }
        }
        ans+=min(abs(n[i]-m[L]), abs(n[i]-m[L+1]));
    }
    cout << ans;
    return 0;
}

by yhcorey @ 2024-08-07 09:30:43

@hkr114514

代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long m,n;
    cin>>m>>n;
    long long a[m+1]={0},b[n+1]={0};
    long long s=0;
    for(int i=1;i<=m;i++){
        cin>>a[i];
        s+=a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
    }
    sort(a+1,a+1+m);
    sort(b+1,b+1+n);
    long long ans=0;
    for(int i=1;i<=n;i++){
        long long head=1;
        long long tail=m;
        long long mid=m/2;
        bool flag=true;
        long long c=m;
        while(head<=tail){
            if(a[mid]>b[i]){
                if(mid<c) c=mid;
                flag=false;
                tail=mid-1;
            }
            else head=mid+1;
            mid=(head+tail)/2;
        }
        long long d=c-1;
        if(d!=0) ans+=min(abs(a[c]-b[i]),abs(a[d]-b[i]));
        else ans+=abs(a[c]-b[i]);
    }
    cout<<ans;
    return 0;
}

by han1219 @ 2024-08-07 09:46:05

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

@hkr114514


by lengfengwcx @ 2024-08-08 16:19:42

这样 求关!!!

#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;
} 

|