Optimal threshold for end of period forecast (202012-2) (50 points + 100 points)

Keywords: Algorithm Machine Learning codeforce AcWing

Problem description

Question No.:202012-2
Test title:Optimal threshold of period end forecast
time limit:1.0s
Memory limit:512.0MB
Problem Description:

Topic background

Considering that the safety index is an integer in a large range and the side dishes are likely to be confused about whether they are really safe, Dunton decided to set a threshold  θ, So that the safety index   y   Into a specific prediction result - "fail" or "fail".

Because the higher the safety index, the less likely it is for students to fail the course   y≥ θ  Dunton will predict that the side dishes will be very safe and will not fail this semester; Conversely, if   y< θ, Dunton would admonish the side dish: "you're going to fail at the end of the term. Don't say you didn't say it beforehand."

So how to set this threshold? Dunton is ready to look for answers from the past.

Title Description

Specifically, Dunton assessed   m   The safety index of students last semester, of which the second   The safety index of students i (1 ≤ i ≤ m) is   yi, it's a   [0,108]   Integer in range; At the same time, the student's failure in the last semester was recorded as   resulti ∈ 0,1, where   0   Indicates failed, 1   Indicates that you have not failed.

Accordingly, use immediately   predict θ (y)   Indicates according to the threshold  θ  Safety index   y   Translate into specific forecast results.
If   predict θ (yj)   And   resultj   Same, the threshold is  θ  Suddenly to the second   j   Whether the students failed the course and predicted correctly; The difference indicates that the prediction is wrong.

predictθ(y)={0(y<θ)1(y≥θ)

Finally, Dunton designed the following formula to calculate the optimal threshold  θ ∗:

θ∗=maxargmaxθ∈yi⁡∑j=1m(predictθ(yj)==resultj)

The formula can also be expressed equivalently as the following rules:

  1. The optimal threshold is only   yi   The safety index is the same as that of a classmate;

  2. This value is adjusted according to this threshold   m   Students failed in the last semester and predicted the most correct times (i.e. the highest accuracy);

  3. When multiple thresholds can reach the highest accuracy, the largest one is selected.

Input format

Read in data from standard input.

The first line of input contains a positive integer   m.

Next enter   m   Line, where   The i (1 ≤ i ≤ m) line includes two integers separated by spaces   yi   and   resulti, as defined above.

Output format

Output to standard output.

Outputs an integer representing the optimal threshold  θ ∗.

Example 1 input

6
0 0
1 0
1 1
3 1
5 1
7 1

Data

Sample 1 output

3

Data

Example 1 explanation

According to rule 1, the selection range of the optimal threshold is   0,1,3,5,7.

θ= 0   When, the number of correct predictions is   4;

θ= one   When, the number of correct predictions is   5;

θ= three   When, the number of correct predictions is   5;

θ= five   When, the number of correct predictions is   4;

θ= seven   When, the number of correct predictions is   3.

The threshold value is selected as   one   or   three   The prediction accuracy is the highest;
Therefore, according to rule 2, the selection range of the optimal threshold is reduced to   1,3.

According to rule 3, θ ∗=max1,3=3.

Example 2 Input

8
5 1
5 0
5 0
2 1
3 0
4 0
100000000 1
1 0

Data

Example 2 output

100000000

Data

Subtask

70%   Test data assurance   m≤200;

All test data assurance   2≤m≤105.

50 points violence solution

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int m;
vector<pair<int , int>>p;
vector<int > s;
int a,b;
int main(){
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        p.push_back({a,b});
        s.push_back(a);
    }
    int id=-1;
    int sum=0;
    int maxsum=0;
    for(int i=0;i<s.size();i++){
        sum=0;
        for(int j=0;j<p.size();j++){
            if((p[j].first>=s[i]&&p[j].second==1)||(p[j].first<s[i]&&p[j].second==0))
            {
                sum++;
            }
        }
        if(maxsum<=sum) {
            maxsum=sum;
            id=p[i].first;
        }
    }
    cout<<id;
//    for(int i=0;i<p.size();i++){
//        cout<<p[i].first<<" "<<p[i].second<<endl;
//    }
}

100 decomposition method

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int n;
pair<int, int>p[N];
int sum[N];
set<int> s;
int main(){
    cin>>n;
    int a,b;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        p[i]=make_pair(a, b);
    }
    sort(p+1, p+1+n);
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+p[i].second;
    int ans,cnt;
    int num1,num2;
    for(int i=1;i<=n;i++){
        int a=p[i].first;
        if(s.count(a)) continue;
        s.insert(a);
        num1=sum[n]-sum[i-1];
        num2=i-1-sum[i-1];
        if((num1+num2)>=ans){
            ans=num1+num2;
            cnt=a;
        }
    }
    cout<<cnt;
}

Posted by brokencode on Thu, 18 Nov 2021 21:38:46 -0800