LightOJ 1027-A Dangerous Maze

题意:

给你n个门,每个门有一个值,如果是正的,那么就代表在x时间后会出去,如果是负的,那么就会回到开始之后的x秒之后。问,最后出去时间的期望。
第一次做期望的dp就碰到这个题,还是看了某大神的解题报告才会,真心弱。
我们可以假设选正数的概率为p1,之后平均花t1的时间出去;选负数的概率是p2,之后平均花t2的时间出去。设期望为TT=p1*t1+p2*(t2+T);
那么T=正数个数的倒数乘以sigma(abs(x[i])).

代码:

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2015 CQU_CST_WuErli. All rights reserved.
//
// #include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define ALL(x) x.begin(),x.end()
#define AT(i,v) for (auto &i:v)
#define For_UVa if (kase!=1) cout << endl
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define look(x) cout << #x << "=" << x
#define SI(a) scanf("%d",&a)
#define SII(a,b) scanf("%d%d",&a,&b)
#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
#define Root 1,n,1
#define BigInteger bign
template <typename T> T max(T& a,T& b) {return a>b?a:b;}
template <typename T> T min(T& a,T& b) {return aint gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
long long gcd (long long a,long long b) {return b==0LL?a:gcd(b,a%b);}
const int MAX_L=2005;// For BigInteger
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7fffffff;
const int MOD=1e9+7;
const double eps=1e-9;
const double pi=acos(-1);
typedef long long  ll;
using namespace std;

int n;
int x[110];
P dp[110][110];

int main(){
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
//  freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    int T_T;
    for (int kase=scanf("%d",&T_T);kase<=T_T;kase++) {
        cin >> n;
        int flag=0;
        int pos=0;
        int sum=0;
        for (int i=1;i<=n;i++) {
            cin >> x[i];
            if (x[i]>0) {
                flag=1;
                pos++;
            }
            sum+=abs(x[i]);
        }
        if (!flag) {
            cout << "Case " << kase << ": inf\n";
            continue;
        }
        int t=__gcd(sum,pos);
        cout << "Case " << kase << ": " << sum/t << '/' << pos/t << endl;
    }
    return 0;
}

你可能感兴趣的:(动态规划)