P1678 10分求助……

P1678 烦恼的高考志愿

行动欲望π @ 2022-08-24 11:46:27

#include<bits/stdc++.h>
using namespace std;
#define int long long
int school[100010],student[100010];
signed main(){
    int m,n,cnt=0;
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    cin>>school[i];
    for(int i=1;i<=n;i++)
    cin>>student[i];
    sort(school+1,school+m+1);
    for(int i=1;i<=n;i++){
        int x=lower_bound(school+1,school+n+1,student[i])-school;
        if(x==1){
            cnt+=abs(school[1]-student[i]);
        }
        if(x==m+1){
            cnt+=abs(school[m]-student[i]);
        }
        else cnt+=min(abs(school[x]-student[i]),abs(school[x-1]-student[i]));
    }   
    cout<<cnt;
}

by KarmaticEnding @ 2022-08-24 11:59:58

#include<bits/stdc++.h>
using namespace std;
int school[100010],student[100010];
int main(){
    long long m,n,discontented=0;
    cin>>m>>n;
    for(long long i=1;i<=m;i++)
    cin>>school[i];
    for(long long i=1;i<=n;i++)
    cin>>student[i];
    for(long long i=1;i<=n;i++){
        long long at_least_discontented=100000;
        for(long long j=1;j<=m;j++){
            if(abs(school[j]-student[i])<at_least_discontented){
                at_least_discontented=abs(school[j]-student[i]);
            }
        }
        discontented+=at_least_discontented;
    }
    cout<<discontented;
}

这不是AC代码!

六点超时一点红


|