80pts,求助,感谢大佬

P1678 烦恼的高考志愿

613102wxllz @ 2024-08-19 20:55:53

详情

#include<bits/stdc++.h>
using namespace std;

long long a[1000100];
long long ans=0;
int main() {
    long long n,m;
    cin>>n>>m;
    for(long long i=1; i<=n; i++) {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    long long y;
    for(long long i=1; i<=m; i++) {
        cin>>y;
        long long u=lower_bound(a+1,a+1+n,y)-a;
        if(a[u]==y){
            ans+=0;
        }
        else{
            if(u!=1){
                long long b=a[u]-y,c=y-a[u-1];
            ans+=min(b,c);
            }
            else{
                ans+=a[u]-y;
            }
        }
    }
    cout<<ans;
    return 0;
}

by Emil_ @ 2024-08-19 20:57:28

@613102wxllz

来了老弟,求关qwq,这次就我一个,ac代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int m,n,a[N],b,s=0;
int main(){
    cin>>m>>n;
    for(int i=1;i<=m;i++)
        cin>>a[i];
    sort(a+1,a+1+m);
    for(int i=1;i<=n;i++){
        cin>>b;
        if(b<=a[1]){
            s=s+a[1]-b;
        }else if(b>=a[m]){
            s=s+b-a[m];
        }else{
            int left=1,right=m;
            while(left<=right){
                int mid=(left+right)/2;
                if(b<=a[mid]){
                    right=mid-1;
                }else{
                    left=mid+1;
                }
            }
            s=s+min(a[left]-b,b-a[left-1]);
        }
    }
    cout<<s;
    return 0;
}

by Dejected @ 2024-08-19 20:57:31

@613102wxllz 完了,老二忘做了 求关qwq


by zhizhenyaohanyu @ 2024-08-19 20:57:39

@613102wxllz

#include<bits/stdc++.h>
using namespace std;
long long a[1000010],x,n,s=0,m;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>m>>n;
    for(int i=0;i<m;i++){
        cin>>a[i];
    }
    sort(a,a+m);
    for(int i=0;i<n;i++){
        cin>>x;
        if(x<=a[0]) s=s+a[0]-x;
        else if(x>=a[m-1]) s=s+x-a[m-1];
        else{
            long long l,r,mid;
            l=0;r=m-1;
            while(l<=r){
                mid=(l+r)/2;
                if(a[mid]>=x) r=mid-1;
                else if(a[mid<x]) l=mid+1;
            }
            int t=min(a[l]-x,x-a[l-1]);
            s+=t;
        }
    }
    cout<<s;
}

求关


by Emil_ @ 2024-08-19 20:58:02

@Dejected 没做你求关,666


by lccjsw @ 2024-08-19 20:58:03

@Emil_ 大哥好帅秒回


by Emil_ @ 2024-08-19 20:59:10

@613102wxllz

玩了,代码现在ac不了了


by zhizhenyaohanyu @ 2024-08-19 20:59:57

@Emil_ 开 longlong


by Emil_ @ 2024-08-19 21:00:06

现在把int改long long就行了


by 613102wxllz @ 2024-08-19 21:03:31

@zhizhenyaohanyu @Emil_ @lccjsw 已关注,求调


|