NC223861. ScholomanceAcademy
描述
输入描述
The first line contains a single integer , the number of instances in the dataset. Then follow lines, each line containing a character and an integer , denoting the actual class and the predicted score of an instance.
It is guaranteed that there is at least one instance of either class.
输出描述
Print the of your classifier within an absolute error of no more than .
示例1
输入:
3 + 2 - 3 - 1
输出:
0.5
示例2
输入:
6 + 7 - 2 - 5 + 4 - 2 + 6
输出:
0.888888888888889
示例3
输入:
8 + 34 + 33 + 26 - 34 - 38 + 39 - 7 - 27
输出:
0.5625
说明:
C++ 解法, 执行用时: 454ms, 内存消耗: 17064K, 提交时间: 2021-07-25 17:01:24
#include<bits/stdc++.h> using namespace std; int n, a, dat[2][1123456], cnt[2]; double ans; char c; signed main() { cin >> n; for(int i = 0; i < n; i++) { cin >> c >> a; dat[c=='+'][cnt[c=='+']++] = -a; } for(int i = 0; i < 2; i++) sort(dat[i], dat[i]+cnt[i]); for(int i = 0; i < cnt[0]; i++) ans += upper_bound(dat[1], dat[1]+cnt[1], dat[0][i]-1) - dat[1]; printf("%.10f", ans / cnt[0] / cnt[1]); }
Python3 解法, 执行用时: 3750ms, 内存消耗: 48888K, 提交时间: 2021-09-30 00:14:50
n=int(input()) neg=[] pos=[] for i in range(n): ch,p=input().split(' ') p=int(p.strip()) if ch=='-': neg.append(p) else: pos.append(p) neg.sort() pos.sort() d=len(pos)*len(neg) k=-1 cnt=0 for i in range(len(pos)): while k+1<len(neg) and neg[k+1]<pos[i]: k=k+1 cnt+=(k+1) print(cnt/d)