70分求为什么错了

P1678 烦恼的高考志愿

Gcy114514 @ 2023-05-17 20:39:21

#include <bits/stdc++.h>
using namespace std;
long long m,n,a[100005],b[100005],sum;
long long find(int x){
    int l=1,r=m,mid;
    while(l<=r){
        mid=(l+r)/2;
        if(x<a[mid]){
            r=mid-1;
        }
        if(x>a[mid]){
            l=mid+1;
        }
        if(x==a[mid]){
            return 0;
        }
    }
    int c[4];
    c[1]=abs(x-a[l]),c[2]=abs(x-a[r]),c[3]=abs(x-a[mid]);
    for(int i=1;i<=3;i++){
        for(int j=1;j<=3;j++){
            if(c[i]<c[j]){
                swap(c[i],c[j]);
            }
        }
    }
    return c[1];
}
int main()
{
    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++){
        cin>>b[i];
        if(m==1)
            sum+=abs(b[i]-a[1]);
        else
            sum+=find(b[i]);
    }
    cout<<sum;
    return 0;
}

by Lost_Boy @ 2023-05-17 20:48:20

#include <bits/stdc++.h>
using namespace std;
template <class T>
void debugVector(const T &a)
{
    cout << "[ ";
    for (size_t i = 0; i < a.size(); ++i)
    {
        cout << a[i] << (i == a.size() - 1 ? " " : ", ");
    }
    cout << "]" << endl;
}
template <class T1, class T2>
void debugVectorPair(const vector<pair<T1, T2>> &a)
{
    cout << "{";
    for (size_t i = 0; i < a.size(); ++i)
    {
        cout << "[ " << a[i].first << ": " << a[i].second << (i == a.size() - 1 ? " ]" : " ], ");
    }
    cout << "}" << endl;
}
template <class T>
void debugMatrix2(const T &a)
{
    for (size_t i = 0; i < a.size(); ++i)
    {
        debugVector(a[i]);
    }
}
template <class T>
using matrix2 = vector <vector <T>>;
template <class T>
vector <vector <T>> getMatrix2(size_t n, size_t m, T init = T())
{
    return vector <vector <T>>(n, vector <T>(m, init));
}
template <class T>
using matrix3 = vector<vector<vector<T>>>;
template <class T>
vector <vector <vector <T>>> getMatrix3(size_t x, size_t y, size_t z, T init = T())
{
    return vector <vector <vector <T>>>(x, vector <vector <T>>(y, vector <T>(z, init)));
}
vector <int> genBigInteger(const string &a)
{
    vector <int> res;
    if (a.empty())
    {
        return res;
    }
    for (int i = a.size() - 1; i >= 0; --i)
    {
        res.push_back(a[i] - '0');
    }
    return res;
}
ostream &printBigInteger(const vector <int> &a)
{
    if (a.empty())
    {
        return cout;
    }
    for (int i = a.size() - 1; i >= 0; --i)
    {
        cout << a[i];
    }
    return cout;
}
vector <int> maxBigInteger(const vector <int> &a, const vector <int> &b)
{
    if (a.size() > b.size())
    {
        return a;
    }
    else if (a.size() < b.size())
    {
        return b;
    }
    for (int i = a.size() - 1; i >= 0; --i)
    {
        if (a[i] > b[i])
        {
            return a;
        }
        else if (a[i] < b[i])
        {
            return b;
        }
    }
    return a;
}
vector <int> addBigInteger(const vector <int> &a, const vector <int> &b)
{
    vector <int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i)
    {
        if (i < a.size())
        {
            pre += a[i];
        }
        if (i < b.size())
        {
            pre += b[i];
        }
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0)
    {
        res.pop_back();
    }
    return res;
}
vector <int> mulBigInteger(const vector <int> &a, int b)
{
    vector <int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || pre; ++i)
    {
        if (i < a.size())
        {
            pre += a[i] * b;
        }
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0)
    {
        res.pop_back();
    }
    return res;
}
vector <int> divBigInteger(const vector <int> &a, int b, int &r)
{
    vector <int> res;
    for (int i = a.size() - 1; i >= 0; --i)
    {
        r = 10 * r + a[i];
        res.push_back(r / b);
        r %= b;
    }
    reverse(res.begin(), res.end());
    while (res.size() > 1 && res.back() == 0)
    {
        res.pop_back();
    }
    return res;
}
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m, n;
    cin >> m >> n;
    vector <int> school(m);
    vector <int> student(n);
    for (int i = 0; i < m; ++i)
    {
        cin >> school[i];
    }
    sort(school.begin(), school.end());
    for (int i = 0; i < n; ++i)
    {
        cin >> student[i];
    }
    long long res = 0;
    for (int i = 0; i < n; ++i)
    {
        int idx = upper_bound(school.begin(), school.end(), student[i]) - school.begin();
        if (idx == m)
        {
            res += abs(school[m - 1] - student[i]);
        }
        else if (idx == 0)
        {
            res += abs(school[0] - student[i]);
        }
        else
        {
            res += min(abs(school[idx] - student[i]), abs(school[idx - 1] - student[i]));
        }
    }
    cout << res << endl;
    return 0;
}

|