问题描述
试题编号: | 202012-2 |
试题名称: | 期末预测之最佳阈值 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: | 题目背景考虑到安全指数是一个较大范围内的整数、小菜很可能搞不清楚自己是否真的安全,顿顿决定设置一个阈值 θ,以便将安全指数 y 转化为一个具体的预测结果——“会挂科”或“不会挂科”。 因为安全指数越高表明小菜同学挂科的可能性越低,所以当 y≥θ 时,顿顿会预测小菜这学期很安全、不会挂科;反之若 y<θ,顿顿就会劝诫小菜:“你期末要挂科了,勿谓言之不预也。” 那么这个阈值该如何设定呢?顿顿准备从过往中寻找答案。 题目描述具体来说,顿顿评估了 m 位同学上学期的安全指数,其中第 i(1≤i≤m)位同学的安全指数为 yi,是一个 [0,108] 范围内的整数;同时,该同学上学期的挂科情况记作 resulti∈0,1,其中 0 表示挂科、1 表示未挂科。 相应地,顿顿用 predictθ(y) 表示根据阈值 θ 将安全指数 y 转化为的具体预测结果。 predictθ(y)={0(y<θ)1(y≥θ) 最后,顿顿设计了如下公式来计算最佳阈值 θ∗: θ∗=maxargmaxθ∈yi∑j=1m(predictθ(yj)==resultj) 该公式亦可等价地表述为如下规则:
输入格式从标准输入读入数据。 输入的第一行包含一个正整数 m。 接下来输入 m 行,其中第 i(1≤i≤m)行包括用空格分隔的两个整数 yi 和 resulti,含义如上文所述。 输出格式输出到标准输出。 输出一个整数,表示最佳阈值 θ∗。 样例1输入 Data 样例1输出 Data 样例1解释按照规则一,最佳阈值的选取范围为 0,1,3,5,7。 θ=0 时,预测正确次数为 4; θ=1 时,预测正确次数为 5; θ=3 时,预测正确次数为 5; θ=5 时,预测正确次数为 4; θ=7 时,预测正确次数为 3。 阈值选取为 1 或 3 时,预测准确率最高; 依规则三,θ∗=max1,3=3。 样例2输入 Data 样例2输出 Data 子任务70% 的测试数据保证 m≤200; 全部的测试数据保证 2≤m≤105。 |
50分暴力解法
//#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分解法
//#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;
}