蒟蒻求助,WA了3个点,求大佬们看一下

P1678 烦恼的高考志愿

Rem_s_Danna @ 2018-02-26 19:59:23

include<bits/stdc++.h>

using namespace std; int s1[100100],s2[100100],s3[100100]; int n,m; bool cmp(int a,int b) { return a<b; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&s1[i]);//xuexiao s1[n+10]=10000000; for(int i=1;i<=m;i++) scanf("%d",&s2[i]);//xueshen sort(s1+1,s1+1+n+10,cmp); sort(s2+1,s2+1+m,cmp); int a=1,b=1; long long asd=0; while(1) { if(s1[a]<=s2[b]) { a++; } else { int z=s1[a]-s2[b]; int x=s2[b]-s1[a-1]; asd+=min(z,x); b++; if(b>m) break; } } printf("%lld",asd); return 0; }


by Rem_s_Danna @ 2018-02-26 20:02:19

#include<bits/stdc++.h>
using namespace std;
int s1[100100],s2[100100],s3[100100];
int n,m;
bool cmp(int a,int b)
{
    return a<b;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&s1[i]);//xuexiao
    s1[n+10]=10000000;
    for(int i=1;i<=m;i++)
        scanf("%d",&s2[i]);//xueshen
    sort(s1+1,s1+1+n+10,cmp);
    sort(s2+1,s2+1+m,cmp);
    int a=1,b=1;
    long long asd=0;
    while(1)
    {
        if(s1[a]<=s2[b])
        {
            a++;
        }
        else
        {
            int z=s1[a]-s2[b];
            int x=s2[b]-s1[a-1];
            asd+=min(z,x);
            b++;
            if(b>m)
                break;
        }
    }
    printf("%lld",asd);
    return 0;
}

|