日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Scholomance Academy 读题训练,模拟(沈阳)

發(fā)布時(shí)間:2025/3/19 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scholomance Academy 读题训练,模拟(沈阳) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題意 :

  • 給出n組,每組包含一個(gè)字符(+/-)和一個(gè)數(shù)值
  • 對(duì)于任意一個(gè)數(shù)值x,如果x大于θ\thetaθ且為正,為TP;x大于θ\thetaθ且為負(fù),為FP;x小于θ\thetaθ且為正,為FN;小于θ\thetaθ且為負(fù),為TN
  • 現(xiàn)定義TPR=TP/(TP+FN)TPR=TP/(TP+FN)TPR=TP/(TP+FN)FPR=FP/(TN+FP)FPR=FP/(TN+FP)FPR=FP/(TN+FP)
  • 可以發(fā)現(xiàn)TPR即所有字符為正的數(shù)值中屬于TP的數(shù)量,FPR…
  • 可以根據(jù)取不同θ\thetaθ時(shí)TPR和FPR的值畫出曲線,要求根據(jù)的ROC曲線,求出AUC(曲線下面積)

思路 :

  • AUC即取到FPR<=1的所有θ\thetaθ對(duì)應(yīng)的TPR這條曲線下的面積,結(jié)合圖例(分段常數(shù)函數(shù))是多個(gè)矩形的面積,由若干段TPR*FPR相加,現(xiàn)在的問題是不知道哪里是分界點(diǎn)
  • 發(fā)現(xiàn)橫縱坐標(biāo)相乘TPR?deltaFPR=(TP?FP)/((TP+FN)?(TN+FP))TPR*deltaFPR=(TP*FP)/((TP+FN)*(TN+FP))TPR?deltaFPR=(TP?FP)/((TP+FN)?(TN+FP)),又發(fā)現(xiàn)分母是所有字符正乘所有字符負(fù),也就是說分母永遠(yuǎn)是常數(shù),因此可以先將分子累加,最后除分母
  • 根據(jù)FPR的值從小到大(由定義公式推出θ\thetaθ從小到大)cmp函數(shù)是從小到大的,若值相同,’+'排在‘-’前,這樣得到的一組坐標(biāo),它的橫縱坐標(biāo)都是遞增的(非嚴(yán)格遞增)),這樣就可以計(jì)算了
  • 將+和-的值分別放在兩個(gè)數(shù)組里,進(jìn)行從小到大的cmp排序
  • a.end() - upper_bound(a.begin(), a.end(), b[i]) 返回大于b[i]的有幾個(gè)
#include <iostream> #include <algorithm> #include <cstring> #include <queue> #define debug(a) cout << #a << " = " << a << endl; #define x first #define y second using namespace std; typedef long long ll;const int N = 1e6 + 10;struct node {char op;int num; }a[N];int n;bool cmp(node a, node b) {if (a.num == b.num) // 如果數(shù)值相同則先出現(xiàn)符號(hào)為-的return a.op < b.op;return a.num < b.num; // 先出現(xiàn)數(shù)值小的 }int main() { // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);int _ = 1; // cin >> _;while (_ -- ){scanf("%d", &n);getchar(); // 同下int po = 0, ne = 0; // 代表正負(fù)總個(gè)數(shù)for (int i = 1; i <= n; i ++ ){scanf("%c%d", &a[i].op, &a[i].num);getchar(); // 換行也是char,scanf不會(huì)自動(dòng)去掉換行if (a[i].op == '+') po ++ ;else ne ++ ;}sort(a + 1, a + n + 1, cmp);double ans = 0, t = 1; // 乘積和 & TP*FP(>theta的正負(fù)乘積)for (int i = 1; i <= n; i ++ ){if (a[i].op == '-') ans += t / ne; // 如果符號(hào)是'-',說明出現(xiàn)轉(zhuǎn)折,乘積和(面積和)加上那部分else t = t - 1.0 / po; // 如果符號(hào)是'+',}printf("%.10lf\n", ans); // 誤差小于等于1e-9}return 0; }

題解二 :

  • 當(dāng)theta變大時(shí),tpr和fpr同時(shí)變大,注意到auc函數(shù)單調(diào)性(當(dāng)r較大時(shí) 我們讓FPR盡量大 這樣可以讓?duì)缺M量大 使得TPR變大),且為若干斜率為0的直線(當(dāng)r在一段較小的區(qū)間內(nèi)波動(dòng)時(shí) FPR不會(huì)變化 如果讓TPR盡量大 就令θ盡量大 此時(shí)TPR也不會(huì)變化),我們可以分割成一個(gè)個(gè)的矩形求面積,在遍歷的時(shí)候?qū)⒑瘮?shù)分成TN+FP段,每一段長度為1/(tn+fp)
//#pragma GCC optimize(2)#include<bits/stdc++.h> //#define FAST ios::sync_with_stdio(false); cin.tie(0); #define int long long #define eps 1e-9using namespace std;const int MAXN = (int)1e6 + 5;struct Node {char op;int val; }a[MAXN];int n, TNFP, TPFN, TP, FP;bool cmp(Node& a, Node& b) {if(a.val == b.val) {if(b.op == '+') return false;if(a.op == '+') return true;}return a.val < b.val; }signed main() {cin >> n;for(int i = 1; i <= n; i++) {cin >> a[i].op >> a[i].val;if(a[i].op == '+') {TPFN++;} else {TNFP++;}}sort(a + 1, a + 1 + n, cmp);TP = TPFN, FP = 0;int tot = 0;for(int i = 1; i <= n; i++) {if(a[i].op == '+') { // 每遇到一個(gè)'+',tp就減少一個(gè)TP--;} else { // 為'-'時(shí)遇到轉(zhuǎn)折點(diǎn),答案累加乘積(tp*fp)tot += TP;}}long double ans = 1.0 * tot / (TPFN * TNFP);printf("%.9Lf", ans);//cout << ans;return 0; }

總結(jié)

以上是生活随笔為你收集整理的Scholomance Academy 读题训练,模拟(沈阳)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。