10 pts 求助 P1678 烦恼的高考志愿

P1678 烦恼的高考志愿

ShanCreeperPro @ 2022-08-23 21:56:15

這題真挺煩惱的(

我的代碼:

#include<bits/stdc++.h>

#define int long long
#define fore(i,x,n) for(int i=x;i<=n;i++)

const int MAXX=100005;
const int mod=1;

inline int __read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}

inline void __write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) __write(x/10);
    putchar(x%10+'0');
}

int n,m;
int Score[MAXX];
int __All=0;
int tmp;

class Student
{
private:
    int Grade;
public:
    Student(int Num);
    int Find();
//protected:

};

Student::Student(int __Num){
    Grade=__Num;
}

inline void input(){
    m=__read(); n=__read();

    fore(i,1,m) Score[i]=__read();

    std::sort(Score+1,Score+1+m);
}

inline int Student::Find(){
    int l=0,r=n+1;
    int __Ans=0x3f3f;

    while(l<=r){
        int mid=(l+r)/2;

        if(Score[mid]==Grade){
            __Ans=0;
            break;
        }

        else if(Score[mid]>Grade){
            __Ans=std::min(__Ans,abs(Score[mid]-Grade));
            r=mid-1;
        }

        else{
            __Ans=std::min(__Ans,abs(Score[mid]-Grade));
            l=mid+1;
        }
    }

    return __Ans;
}

signed main(){

#ifdef LOCAL
    freopen("debug_data.in","r",stdin);
    freopen("debug_data.out","w",stdout);
#endif

    input();

    fore(i,1,n){
        tmp=__read();

        Student S(tmp);
        __All+=S.Find();
    }

    __write(__All);
}

對著題解改都快改成一樣的了

Find 函數是個二分,#ifdef 那幾句對程式沒有影響。

樣例全對,包括自造樣例。

蚌。


by xzy090626 @ 2022-08-23 21:58:35

@ShanCreeper 为什么不在群里问(


by ShanCreeperPro @ 2022-08-23 21:59:07

@xzy090626

因爲我的寫法他們肯定不會給我調的 /oh /hsh


by xzy090626 @ 2022-08-23 21:59:39

我看看啊


by ShanCreeperPro @ 2022-08-23 22:00:14

@xzy090626

飄散的碼風(


by 览遍千秋 @ 2022-08-23 22:02:39

@ShanCreeper 不在群里问我就看不到了吗 /hsh


by ShanCreeperPro @ 2022-08-23 22:03:17

@expect2004

class 寫的怕被你們說 /hsh


by xzy090626 @ 2022-08-23 22:04:22

@ShanCreeper 更想说的难道不是

#define int long long

吗(


by xzy090626 @ 2022-08-23 22:05:53

@ShanCreeper 您认为 0x3f3f 足够吗?


by irris @ 2022-08-23 22:06:47

@ShanCreeper Student::Find() n + 1 應該改成 m + 1 吧


by xzy090626 @ 2022-08-23 22:08:33

似乎还要特判一下


| 下一页