题目背景

考虑到安全指数是一个较大范围内的整数、小菜很可能搞不清楚自己是否真的安全,顿顿决定设置一个阈值 θ,以便将安全指数 y 转化为一个具体的预测结果——“会挂科”或“不会挂科”。

因为安全指数越高表明小菜同学挂科的可能性越低,所以当 y≥θ 时,顿顿会预测小菜这学期很安全、不会挂科;反之若 y<θ,顿顿就会劝诫小菜:“你期末要挂科了,勿谓言之不预也。”

那么这个阈值该如何设定呢?顿顿准备从过往中寻找答案

题目描述

具体来说,顿顿评估了 m 位同学上学期的安全指数,其中第 i(1≤i≤m)位同学的安全指数为 yi,是一个 [0,108] 范围内的整数;同时,该同学上学期的挂科情况记作 resulti∈0,1,其中 0 表示挂科、1 表示未挂科。

相应地,顿顿用 predictθ(y) 表示根据阈值 θ 将安全指数 y 转化为的具体预测结果。
如果 predictθ(yj) 与 resultj 相同,则说明阈值为 θ 时顿顿对第 j 位同学是否挂科预测正确;不同则说明预测错误。

最后,顿顿设计了如下公式来计算最佳阈值 θ∗

 

该公式亦可等价地表述为如下规则:

  1. 最佳阈值仅在 yi 中选取,即与某位同学的安全指数相同;

  2. 按照该阈值对这 m 位同学上学期的挂科情况进行预测,预测正确的次数最多(即准确率最高);

  3. 多个阈值均可以达到最高准确率时,选取其中最大的。

输入格式

从标准输入读入数据。

输入的第一行包含一个正整数 m。

接下来输入 m 行,其中第 i(1≤i≤m)行包括用空格分隔的两个整数 yi 和 resulti,含义如上文所述

输出格式

输出到标准输出。

输出一个整数,表示最佳阈值 θ∗。

样例输入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。

依规则三,θ∗=max1,3=3

 

样例输入2

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

样例输出2

100000000

子任务

70% 的测试数据保证 m≤200;

全部的测试数据保证 2≤m≤105

 解题思路:

2≤m≤105,两层循环得分70,代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
struct student{
    int theta;//阈值
    int result;//是否挂科
    int account;//正确个数 
};
bool com1(student a,student b)
{
    if(a.theta==b.theta)
        return a.result<b.result;
    return a.theta<b.theta;
} 
bool com2(student a,student b)
{
    if(a.account==b.account)
        return a.theta>b.theta;
    return a.account>b.account;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    student s[n];
    int a,b;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b;
        s[i].theta=a;
        s[i].result=b;
    }
    sort(s,s+n,com1);
    int sum;
    for(int i=0;i<n;i++)
    {
        sum=0;
        for(int j=0;j<i;j++)
        {
            if(s[i].theta==s[j].theta)
            {
                if(s[j].result==1)
                    sum++;
            }
            else
                if(s[j].result==0)
                    sum++;
        }
        for(int k=i;k<n;k++)
        {
            if(s[i].theta==s[k].theta)
            {
                if(s[k].result==1)
                    sum++;
            }
            else
                if(s[k].result==1)
                    sum++;
        }
        s[i].account=sum;
    }
    sort(s,s+n,com2);
    cout<<s[0].theta;
    return 0;
}

所以采用前缀和求解,AC代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
struct student{
    int theta;//阈值
    int result;//是否挂科
    int count_0=0;//预测挂科正确数目
    int count_1=0;//预测不挂科正确数目
    int sum=0;//预测正确总和 
};
bool com1(student a,student b)
{
    if(a.theta==b.theta)
        return a.result<b.result;
    return a.theta<b.theta;
} 
bool com2(student a,student b)
{
    if(a.sum==b.sum)
        return a.theta>b.theta;
    return a.sum>b.sum;
}
int main()
{
    std::ios::sync_with_stdio(false);//快速输入 
    int n;
    cin>>n;
    student s[n];
    int a,b;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b;
        s[i].theta=a;
        s[i].result=b;
    }
    sort(s,s+n,com1);
    if(s[0].result==0) 
        s[0].count_0++;//第一个人的前缀0个数 
    if(s[n-1].result==1)
        s[n-1].count_1++;//最后一个人的前缀1个数
    for(int i=1;i<n;i++)//统计每个人的前缀0个数 
    {
        if(s[i].result==0)
            s[i].count_0=s[i-1].count_0+1;
        else s[i].count_0=s[i-1].count_0;
    }
    for(int i=n-2;i>=1;i--)//统计每个人的前缀1个数 
    {
        if(s[i].result==1)
            s[i].count_1=s[i+1].count_1+1;
        else s[i].count_1=s[i+1].count_1;
    }
    for(int i=1;i<n;i++)//计算每个阈值的预测正确数 
    {
        if(s[i].theta==s[i-1].theta)
            continue;//若阈值相同则不用重复统计 
        s[i].sum=s[i-1].count_0+s[i].count_1; 
    } 
    sort(s,s+n,com2);
    cout<<s[0].theta;
    return 0;
}

 

posted on 2021-04-10 12:11  liyou555  阅读(239)  评论(0编辑  收藏  举报