P1678,求DALAO(初中生的疑惑)

P1678 烦恼的高考志愿

linhe4034 @ 2021-01-31 22:26:24

求DALAO看一看

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int school,student,start=0;
int range_score[1000010],answer=0;
int school_score[1000010];
int main(){
    cin>>school>>student;
    for(int i=0;i<school;i++){cin>>school_score[i];}
    for(int i=0;i<student;i++){cin>>range_score[i];}
    sort(range_score,range_score+student);
    sort(school_score,school_score+school);
    for(int i=0;i<student;i++){
        for(int j=start;j<school;j++){
            if(school_score[j]>range_score[i]){
                if(range_score[i]-school_score[j-1]>=school_score[j]-range_score[i]){
                    answer+=school_score[j]-range_score[i];
                    start=school_score[j];
                }else{
                    answer+=range_score[i]-school_score[j-1];
                    start=school_score[j-1];
                }
                break;
            }
        }
    }
    cout<<answer;
    return 0;
}

急,在线等!!!!!! !!!!!!!!!!!!! !!!!!!


by Jur_Cai @ 2021-01-31 22:49:29

if(range_score[i]-school_score[j-1]>=school_score[j]-range_score[i]) {
    answer+=school_score[j]-range_score[i];
    start=school_score[j];
} else {
    answer+=range_score[i]-school_score[j-1];
    start=school_score[j-1];
}

start的值赋错了吧。。。


by Jur_Cai @ 2021-01-31 22:53:20

school_score[j-1]

这个东西j==0时越界了


by Jur_Cai @ 2021-01-31 22:54:51

也没有处理学生估分大于最大分数线的情况。。。


by linhe4034 @ 2021-01-31 23:37:21

@违规用户rhz 谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


|