一、题目描述
样例1输入 6 0 0 1 0 1 1 3 1 5 1 7 1 样例1输出 3 样例1解释 按照规则一,最佳阈值的选取范围为0,1,3,5,7。 当阈值为0时,预测正确次数为4; 当阈值为1时,预测正确次数为5; 当阈值为3时,预测正确次数为5; 当阈值为5时,预测正确次数为4; 当阈值为7时,预测正确次数为3。 阈值选取为1或 3时,预测准确率最高; 所以按照规则二,最佳阈值的选取范围缩小为1或3。 依规则三,最佳阈值为3. 样例2输入 8 5 1 5 0 5 0 2 1 3 0 4 0 100000000 1 1 0 样例2输出 100000000二、代码实现
#include #include using namespace std; struct Student{ int y; int result; }; bool cmp(Student s1,Student s2) { return s1.y < s2.y; } int main() { Student student[100005]; int count_0[100005]={0}; int count_1[100005]={0}; //输入数据,存到结构体数组中 int m; cin>>m; for(int i=0;i<m;i++) { cin>>student[i].y>>student[i].result; } //排序,由y值从小到大 sort(student,student+m,cmp); //统计小于每个student[i].y的0的个数,也就是预测结果和实际结果都为0的个数 int i = 0 , j = 1; int coing_0 = 0 , coing_1 = 0; while(j<m) { if(student[j].y==student[i].y) { //避免漏掉相同y值的计数情况 j++; continue; } //从i到j统计累计的0的个数,并加上之前已有的0的个数(temp) int temp = 0; while(i<j) { if(student[i].result==0) temp++; count_0[i] = coing_0; i++; } coing_0 += temp; } //统计最后几个,避免j达到m之后而漏掉了最后几个数据 while(i<j) { count_0[i] = coing_0; i++; } //统计大于等于每个student[i].y的1的个数,也就是预测结果和实际结果都为1的个数 for(int i=m-1;i>=0;i--) { if(student[i].result==1) coing_1++; count_1[i] = coing_1; } //输出最大阈值 int ans = student[0].y; int num = count_0[0] + count_1[0]; for(int i=1;i<m;i++) { if(count_0[i] + count_1[i] >= num) { num = count_0[i] + count_1[i]; ans = student[i].y; } } cout<<ans<<endl; return 0; }
更多CCFCSP认证真题详解,请点击>>CCFCSP历年认证考试真题解答汇总
