Day11 - F - A Dangerous Maze LightOJ - 1027

求期望注意期望的定义,这题我们可以分正负数情况,设所求期望为E

正数: 1/n*x_i

负数:1/n*(E+x_j) 此时概率为1/n,根据期望定义,他回到起点后出去的期望为E,花费回起点的时间为x_j,也就是该点的取值情况

整理上式 E = sum / cnt_i   sum是所有绝对值和, cnt_i是正数数量

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;

const int maxm = 105;
int buf[maxm];

inline int gcd(int a, int b) {
    return b == 0?a:gcd(b,a%b);
}

void run_case() {
    int n; cin >> n;
    bool flag = false;
    int ans = 0, cnt = 0;
    for(int i = 0; i < n; ++i) {
        cin >> buf[i];
        ans += abs(buf[i]);
        if(buf[i] > 0) cnt++;
    }
    if(cnt) {
        int div = gcd(ans, cnt);
        cout << ans/div << "/" << cnt / div << "\n";
    } else {
        cout << "inf\n";
    }
}
 
int main() {
    //ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(10);
    int t; cin >> t;
    //while(t--)
    for(int i = 1; i <= t; ++i) {
        cout << "Case " << i << ": ";
        run_case();
    }
    //cout.flush();
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/GRedComeT/p/12343118.html
今日推荐