救命

P1678 烦恼的高考志愿

yuechenxi130407 @ 2024-10-23 09:46:00

#include <bits/stdc++.h>
using namespace std;
int m,n,num,ans,sch[100001];
int main(){
    cin>>m>>n;
    for (int i=1;i<=m;++i) cin>>sch[i];
    sort(sch+1,sch+m+1);
    for (int i=1;i<=n;++i){
        int l=1,r=m,ls=1000001;
        cin>>num;
        while (l<=r){
            int mid=(l+r)/2;
            if (sch[mid]==num){
                ls=0;
                break;
            }
            else if(sch[mid]>num){
                ls=min(ls,sch[mid]-num);
                r=mid-1;
            }
            else{
                ls=min(ls,num-sch[mid]);
                l=mid+1;
            }
        }
        ans+=ls;
    }
    cout<<ans;
    return 0;
}

WA on #11


by Ff472130 @ 2024-10-23 10:00:40

不开long long导致的

#include <bits/stdc++.h>
using namespace std;
long long m,n,num,ans,sch[100001];
int main(){
    cin>>m>>n;
    for (int i=1;i<=m;++i) cin>>sch[i];
    sort(sch+1,sch+m+1);
    for (int i=1;i<=n;++i){
        long long l=1,r=m,ls=1000001;
        cin>>num;
        while (l<=r){
            int mid=(l+r)/2;
            if (sch[mid]==num){
                ls=0;
                break;
            }
            else if(sch[mid]>num){
                ls=min(ls,sch[mid]-num);
                r=mid-1;
            }
            else{
                ls=min(ls,num-sch[mid]);
                l=mid+1;
            }
        }
        ans+=ls;
    }
    cout<<ans;
    return 0;
}

by ZHANGYUNZENG2012 @ 2024-10-26 14:36:41

十年OI一场空,不开long long 见祖宗


|