就我40分?lower_bound

P1678 烦恼的高考志愿

Zpril_20211017 @ 2022-12-20 19:52:58

#include <bits/stdc++.h>
using namespace std;

long long a[100010];
long long b[100010];

int main()
{
    long long m,n,cnt = 0;
    cin >> m >> n;
    a[0] = 1145141919810;
    a[m] = 1145141919810;
    for(long long i = 1;i <= m;i++)
        cin >> a[i];
    sort(a + 1,a + m + 1);
    for(long long i = 1;i <= n;i++)
    {
        cin >> b[i];
        long long l = lower_bound(a + 1,a + m + 1,b[i]) - a;
        cnt += min(a[l] - b[i],b[i] - a[l - 1]);
    }
    cout << cnt;
    return 0;
}

by Night_sea_64 @ 2022-12-20 20:01:48

@Zpril 您那个 a[m]=1145141919810 是不是设错了?应该是 m+1 吧我觉得


by Zpril_20211017 @ 2022-12-20 20:06:32

@Netherite_sword_666 不是无穷大吗


by Night_sea_64 @ 2022-12-20 20:12:17

@Zpril a[m+1]=1145141919810


by Zpril_20211017 @ 2022-12-21 11:17:56

@Netherite_sword_666 还是40分:(


by Night_sea_64 @ 2022-12-21 11:19:39

@Zpril 那您试试不开那么多 longlong。。。我也调不出来


by Night_sea_64 @ 2022-12-21 11:21:03

@Zpril 参考我的代码:

#include<iostream>
#include<algorithm>
using namespace std;
int a[100010];
int main()
{
    int m,n;
    long long sum=0;
    cin>>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++)
    {
        int b;
        cin>>b;
        int pos=lower_bound(a+1,a+m+1,b)-a;
        if(pos==m+1)sum+=b-a[m];
        else if(pos==1)sum+=a[1]-b;
        else sum+=min(abs(a[pos]-b),abs(a[pos-1]-b));
    }
    cout<<sum<<endl;
    return 0;
}

by Night_sea_64 @ 2022-12-21 11:23:03

@Zpril 哦对 b[i]-a[i-1] 有可能减去的是 1145141919810


by Night_sea_64 @ 2022-12-21 11:23:53

说错了,-a[l-1]


by Zpril_20211017 @ 2023-01-08 19:50:40

thx


|