用的二分还是超时了求助!!qwq

P1678 烦恼的高考志愿

Lohax_ @ 2022-02-15 17:33:00

用的二分还是超时了求助!!qwq


by Lohax_ @ 2022-02-15 17:35:33

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"math.h" 
int main(void){
    long long n,m,i,j,t,k,l,r,x=0;
    long long ans=0;
    scanf("%lld%lld",&m,&n);
    long long a[m],b[n];
    for(i=0;i<m;i++) scanf("%lld",&a[i]);
    for(i=0;i<m;i++) 
        for(j=i+1;j<m;j++)
            if(a[i]>a[j]){
                t=a[j];
                a[j]=a[i];
                a[i]=t;
            }
    for(i=0;i<n;i++) scanf("%lld",&b[i]);
    for(i=0;i<n;i++){
        l=0,r=m-1,t=999999;
        while(l<=r){
            k=l+r>>1;
            if(a[k]-b[i]>=0) r=k-1;
            else l=k+1;
            if(t>(long long)abs(a[k]-b[i])) t=(long long)abs(a[k]-b[i]);
        }
        ans+=t;
    }
    printf("%lld",ans); 
}    

by Azure__ @ 2022-02-15 17:41:42

用sort排序试试; 还有,应该是k=(l+r)>>1; 因为位运算的优先程度很低,要打括号


by Yukinoshita_Yukino @ 2022-02-15 17:45:29

排序复杂度不对


by Keep_RAD @ 2022-02-15 17:48:36

@Azure__ 不用打,位运算等级低先算加法


by Keep_RAD @ 2022-02-15 17:48:58

这年头谁用冒泡啊


by Azure__ @ 2022-02-15 17:50:02

我提交了,这样改就AC了


by Lohax_ @ 2022-02-15 18:28:20

谢谢,原来是排序出了问题。涨知识了,原来还有快速排序的算法。


by Macesuted @ 2022-02-15 18:47:23

哇撞头了


by Lolita_Peach @ 2022-02-22 20:42:00

肯定要用sort了


|