60分求助,2,3,8,10WA!!!!!在线等,急!!!

P1678 烦恼的高考志愿

billifs2 @ 2022-08-06 11:50:01

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100000],b,c,xx;
int main(){
    scanf("%d%d",&m,&n);;
    for(int i=1;i<=m;i++)cin>>a[i];
    sort(a+1,a+m+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b);
        int l=1,r=m,mid;
        while(l<r)
        {
            mid=(l+r)/2;
            if(b>a[mid])l=mid+1;
            else r=mid; 
        }
        c=abs(b-a[l]);
        c=min(c,abs(b-a[l-1]));
        xx+=min(abs(b-a[l+1]),c);
    }
    printf("%d",xx);
    return 0;
}

by s_c_lmlm_L @ 2022-08-06 11:56:39

@billifs2 a多开10就能70了(doge)


by chienyu @ 2022-08-06 11:59:48

@billifs2

while(l + 1 < r)

by chienyu @ 2022-08-06 12:10:36

if(b[i]<=a[1])
    xx+=a[1]-b;
else
    xx+=min(abs(a[l-1]-b),abs(a[l]-b));

加个特判逝世?


by billifs2 @ 2022-08-06 14:22:10

@chienyu 加在哪

c=abs(b-a[l]);
c=min(c,abs(b-a[l-1]));
xx+=min(abs(b-a[l+1]),c);

要删掉吗


by billifs2 @ 2022-08-06 14:35:05

@YangjinRun


by billifs2 @ 2022-08-06 14:38:23

@chienyu


by chienyu @ 2022-08-06 14:44:52

删一下逝世


|