求救最后一个测试点没有通过

P1678 烦恼的高考志愿

Submerge69 @ 2024-02-27 17:05:02

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010],b[100010];
long long num;
int f;
void check(int temp){
    int l=1,r=m,mid;
    f=abs(a[1]-temp);
    while(l<=r){
        mid=l+(r-l)/2;
        if(a[mid]>temp) r=mid-1;
        else l=mid+1;
        f=abs(temp-a[mid]);
    }
    if(mid+1<=m) f=min(f,abs(temp-a[mid+1]));
    if(mid-1>=1) f=min(f,abs(temp-a[mid-1]));
    return ;
}

int main(){
    scanf("%d %d",&m,&n);
    for(int i=1;i<=m;i++) scanf("%d",&a[i]);//录取线 
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);//成绩 
    sort(a+1,a+m+1); 
    for(int i=1;i<=n;i++){
        f=2000000;
        check(b[i]);
        num+=f;
    }
    printf("%d",num);
    return 0;
} 

by Orz___zrO @ 2024-02-27 18:01:27

#include<bits/stdc++.h>
using namespace std;
int n,m,a[100010],b[100010];
long long num;
int f;
void check(int temp){
    int l=1,r=m,mid;
    f=abs(a[1]-temp);
    while(l<=r){
        mid=l+(r-l)/2;
        if(a[mid]>temp) r=mid-1;
        else l=mid+1;
        f=abs(temp-a[mid]);
    }
    if(mid+1<=m) f=min(f,abs(temp-a[mid+1]));
    if(mid-1>=1) f=min(f,abs(temp-a[mid-1]));
    return ;
}

int main(){
    scanf("%d %d",&m,&n);
    for(int i=1;i<=m;i++) scanf("%d",&a[i]);//录取线 
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);//成绩 
    sort(a+1,a+m+1); 
    for(int i=1;i<=n;i++){
        f=2000000;
        check(b[i]);
        num+=f;
    }
    printf("%d",num);
    return 0;
} 

这样就过了


by Orz___zrO @ 2024-02-27 18:02:02

@scyxk 求关dingzj2022


by Submerge69 @ 2024-02-27 19:48:12

@Orz___zrO 最后一个测试点还是没通过啊?


by zhenghbkh @ 2024-02-28 21:42:20

建议用 ```cpp cout << num << endl;


by zhenghbkh @ 2024-02-28 21:43:10

不好意思,是cout


by Orz___zrO @ 2024-03-01 18:01:17

@scyxk 我都过了啊link


by __zhang__ @ 2024-03-10 09:08:46

开long long


by __zhang__ @ 2024-03-10 09:09:47

不好意思,看错了


by __zhang__ @ 2024-03-10 09:10:20

可以这样

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100005],b[100005];
int n,m;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    for(int i=0;i<m;i++) scanf("%d",&b[i]);
    sort(a,a+n);
    long long tot=0;
    for(int i=0;i<m;i++){
        int zuo=0,you=n-1,ans=0x7fffffff,now=0;
        while(zuo<=you){
            int c=(zuo+you)/2;
            if(a[c]<b[i]){
                ans=min(ans,abs(b[i]-a[c]));
                zuo=c+1;
            }
            else{
                you=c-1;
                ans=min(ans,abs(b[i]-a[c]));
            }
        }
        tot+=ans;
    }
    printf("%lld",tot); 

}

by __zhang__ @ 2024-03-10 09:11:01

@zhenghbkh 这样可能更慢


| 下一页