期末预测之最佳阈值

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct node {
 5     int y,result;
 6 };
 7 vector<node> nodes;
 8 
 9 bool cmp(node &a,node &b) {
10     if(a.y != b.y) return a.y < b.y;
11     else return a.result < b.result;
12 }
13 vector<long int> cnt_0,cnt_1;
14 int main() {
15     int m,y,result;
16     scanf("%d",&m);
17     for(int i = 0 ; i < m; ++i) {
18         scanf("%d%d",&y,&result);
19         nodes.push_back({y,result});
20     }
21     sort(nodes.begin(),nodes.end(),cmp); //先对输入排序
22     
23     //cnt_0存储y前面result为0的个数
24     //cnt_1存储y前面result为1的个数 
25     cnt_0.resize(m);
26     cnt_1.resize(m);
27     if(nodes[0].result == 0) {
28         cnt_0[0] = 1;
29         cnt_1[0] = 0;
30     } else {
31         cnt_0[0] = 0;
32         cnt_1[0] = 1;
33     }
34     for(int i = 1; i < nodes.size(); ++i) {
35         if(nodes[i].result == 0) {
36             cnt_0[i] = cnt_0[i-1] + 1;
37             cnt_1[i] = cnt_1[i-1];
38         } else {
39             cnt_1[i] = cnt_1[i-1] + 1;
40             cnt_0[i] = cnt_0[i-1];
41         }
42     }
43 
44     // 初始化
45     int final_y = nodes[0].y;
46     long int final_cnt = cnt_1[m-1];
47 
48     for(int i = 1; i < m; ++i) {
49         long int current_cnt = 0;
50         while(nodes[i].y == nodes[i-1].y && i < m) //如果前面计算过y,则跳过 
51             ++i;
52         current_cnt = cnt_0[i-1] + cnt_1[m-1] - cnt_1[i-1]; //y的正确预测次数 = y前面result为0的个数 +  y以及y后面result为1的个数 
53         if(current_cnt >= final_cnt) {
54             final_cnt = current_cnt;
55             final_y = nodes[i].y;
56         }
57     }
58     printf("%d",final_y);
59     return 0;
60 }

 

posted @ 2021-04-07 15:42  tangq123  阅读(391)  评论(0编辑  收藏  举报