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 _i_i_ @ 2022-08-23 22:09:54

@ShanCreeper 改的有点多

#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(long long x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) __write(x/10);
    putchar(x%10+'0');
}

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

class Student
{
private:
    long long 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);

    Score[0] = -0x3f3f3f3f; Score[m + 1] = 0x3f3f3f3f;
}

inline int Student::Find(){
    int l=0,r=m+1;
    long long __Ans=0x3f3f3f3f;

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

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

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

        else{
            __Ans=std::min(__Ans,(long long)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);
}

by irris @ 2022-08-23 22:10:02

@xzy090626 需要特判什麽呢 /oh


by _i_i_ @ 2022-08-23 22:10:58

@AlgorithmerSnow 边界没有设定即为 0,会对答案造成影响


by irris @ 2022-08-23 22:11:41

@__uint32_t 雖然如此,那我覺得是他自己的實現問題。


by bug退散 @ 2022-08-23 22:11:43

老师:建议不要炫技/hsh


by xzy090626 @ 2022-08-23 22:12:24

啊其实我们群里正在讨论 @ShanCreeper 使用 class 的问题(


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

100 pts,此帖結


by 230syh @ 2022-11-01 20:53:30

~~多 此~~

亿


上一页 |