大佬求调,阿里嘎多,thank you,思密达,70分,WA

P1678 烦恼的高考志愿

I_am_wjr_I_AK_IOI @ 2024-07-10 09:03:30

#include<bits/stdc++.h>
#define LL long long  
using namespace std;
int n,k,h[1000010],w[1000010],m,ans,sss=1;
int cmp(int x,int y){
    return x<y;
}
int main()
{

    cin>>m>>n;
    for(int i=1;i<=m;i++){
        cin>>h[i];
    }

    for(int i=1;i<=n;i++){
        cin>>w[i];
    }
    sort(w+1,w+n+1,cmp);
    sort(h+1,h+m+1,cmp);
    h[0]=0;
    h[m+1]=1000000;
    for(int i=0;i<=m;i++){
        for(int j=sss;j<=n;j++){
            if((h[i]<=w[j])&&(w[j]<=h[i+1])){
                ans+=min((w[j]-h[i]),(h[i+1]-w[j]));
            }
            if(h[i+1]<w[j]){
                sss=j;
                break;
            }
        }
    }
    cout<<ans;
    return 0;   
}

by linmoxi @ 2024-07-12 22:15:28

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<string>
using namespace std;
const int N=1e5+5;
int a[N],b[N];
long long cnt;
int m,n;
long long find(int h){
    if(h<=a[1])return a[1]-h;
    if(h>=a[m])return h-a[m];
    int l=1,r=m;
    while(l+1<r){
        int mid=l+r>>1;
        if(h>=a[mid])l=mid;
        else r=mid;
    }
    return min(h-a[l],a[r]-h);
}
int main(){
    cin>>m>>n;
    for(int i=1;i<=m;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
    }
    sort(a+1,a+1+m);
    //cout<<b[1]<<" "<<a[m];
    for(int i=1;i<=n;i++){
        int p;
        //cout<<find(b[i])<<endl;
        cnt+=find(b[i]);
    }
    cout<<cnt;
    return 0;
}

by zldx @ 2024-07-18 15:05:50

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

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    T n,m,sum=0;
    cin>>n>>m;
    vector<T>v1(n);
    vector<T>v2(m);
    for(T i=0;i<n;i++){
        cin>>v1[i];
    }
    sort(v1.begin(), v1.end());
    for(T i=0;i<m;i++){
        cin>>v2[i];
    }
    sort(v2.begin(), v2.end());
    for(T i=0;i<m;i++){
//        cout<<"@";
        if(v2[i]<v1[0]){
            sum+=v1[0]-v2[i];
            continue;
        }if(v2[i]>v1[n-1]){
            sum+=v2[i]-v1[n-1];
            continue;
        }
        T r,l,mid;
        l=0;
        r=n-1;
        while(l<r){
//            cout<<"#";
            mid=(l+r+1)/2;
//            cout<<mid;
            if((v1[mid]<v2[i]&&v1[mid+1]>v2[i])||(v1[mid]>v2[i]&&v1[mid-1]<v2[i])){
                if(v1[mid]<v2[i]&&v1[mid+1]>v2[i]){
//                    cout<<"|";
                    sum+=min(v2[i]-v1[mid],v1[mid+1]-v2[i]);
//                    cout<<sum<<endl;
                    break;
                }else{
//                    cout<<"}";
                    sum+=min(v2[i]-v1[mid-1],v1[mid]-v2[i]);
//                    cout<<sum<<endl;
                    break;
                }
            }else{
                if(v1[mid]<v2[i]){
//                    cout<<")";
                    l=mid+1;
                }if(v1[mid]>v2[i]){
//                    cout<<"?";
                    r=mid-1;
                }if(v1[mid]==v2[i]){
//                    cout<<"*";
//                    cout<<sum<<endl;
                    break;
                }
            }
        }
    }
    cout<<sum;
    return 0;
}

|