Weather Patterns(2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 A)

Problem Description

Consider a system which is described at any time as being in one of a set of N distinct states, 1,2,3,...,N. We denote the time instants associated with state changes as t = 1,2,..., and the actual state at time t as a_{ij}=p=[s_{i}=j\ |\ s_{i-1}=i], 1\le i,j \le N. For the special case of a discrete, first order, Markovchain, the probabilistic description for the current state (at time tt) and the predecessor state is st​. Furthermore we only consider those processes being independent of time, thereby leading to the set of state transition probability aij​of the form: with the properties a_{ij}​≥0 and \sum_{i=1}^{N} A_{ij} = 1. The stochastic process can be called an observable Markovmodel. Now, let us consider the problem of a simple 4-state Markov model of weather. We assume that once a day (e.g., at noon), the weather is observed as being one of the following:

State 1: snow

State 2: rain

State 3: cloudy

State 4: sunny

The matrix A of state transition probabilities is:

te transition probabilities is:

A = \{a_{ij}\}= \begin{Bmatrix} a_{11}&a_{12}&a_{13}&a_{14} \\ a_{21}&a_{22}&a_{23}&a_{24} \\ a_{31}&a_{32}&a_{33}&a_{34} \\ a_{41}&a_{42}&a_{43}&a_{44} \end{Bmatrix}

Given the model, several interesting questions about weather patterns over time can be asked (and answered). We canask the question: what is the probability (according to the given model) thatthe weather for the next k_ days willbe? Another interesting question we can ask: given that the model is in a knownstate, what is the expected number of consecutive days to stay in that state?Let us define the observation sequence O as O =\left { s{1}, s_{2}, s_{3}, ... , s_{k} \right }, and the probability of the observation sequence O given the model is defined as p(O|model). Also, let the expected number of consecutive days to stayin state i be Ei​. Assume that the initial state probabilities p[s_{1} = i] = 1, 1 \leq i \leq N. Both p(O∣model) and Ei​ are real numbers.

Input

Line 1~4 for the state transition probabilities. Line 5 for the observation sequence O1​, and line 6 for the observation sequence O2​. Line 7 and line 8 for the states of interest to find the expected number of consecutive days to stay in these states.

Line 1: a_{11}\ a_{12}\ a_{13}\ a_{14}

Line 2: a_{21}\ a_{22}\ a_{23}\ a_{34}

Line 3: a_{31}\ a_{32}\ a_{33}\ a_{34}

Line 4: a_{41}\ a_{42}\ a_{43}\ a_{44}

Line 5: s_{1}\ s_{2}\ s_{3}\ ...\ s_{k}

Line 6: s_{1}\ s_{2}\ s_{3}\ ...\ s_{l}

Line 7: i

Line 8: j

Output

Line 1 and line 2 are used to show the probabilities of the observation sequences O1​ and O2​ respectively. Line 3 and line 4 are for the expected number of consecutive days to stay in states ii and jj respectively.

Line 1: p[O1​∣model]

Line 2: p[O2​∣model]

Line 3: Ei​

Line 4: Ej​

Please be reminded that the floating number should accurate to 10−8.

Sample Input

0.4 0.3 0.2 0.1
0.3 0.3 0.3 0.1
0.1 0.1 0.6 0.2
0.1 0.2 0.2 0.5
4 4 3 2 2 1 1 3 3
2 1 1 1 3 3 4
3
4

​​​​​​​Sample ​​​​​​​Output

0.00004320
0.00115200
2.50000000
2.00000000

题意:已知 1、2、3、4 分别表示一种天气状况,再给出一个 4*4 的一阶马尔科夫状态转移概率矩阵 G 用于表示天气之间转移的概率,现给出两个数串 a、b,问从数串的第一个数依次变换到最后一个数的概率是多少,最后再给出两个整数,分别代表两个天气状况 E1、E2,问该天气趋于平稳状态下的数学期望

思路:

一个阅读理解题,哪怕不了解马尔科夫链,读懂题后也能做

根据马尔科夫状态转移概率矩阵的性质,第 i 行表示当前观测的值,第 j 行表示现在观测的值,由于马尔科夫链转移过程中后一状态仅受前一状态的影响,因此对于第一问分别求两个数串转移概率,只要不断的记录 G[a[i]][a[i+1]] 的值然后相乘即可

当马尔科夫状态转移概率矩阵经过一系列的转移后,最后会趋于一个平稳状态,转移的数学期望也就变成一个定值,因此只要不断的令 G[E1][E1] 与自己相乘,再将相乘后的结果相加即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

double G[N][N];
int a[N],cntA;
int b[N],cntB;
int main(){
    char ch;
    int E1,E2;
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            scanf("%lf",&G[i][j]);
    getchar();
    while((ch=getchar())!='\n'){
        if(ch!=' ')
            a[++cntA]=ch-'0';
    }
    while((ch=getchar())!='\n'){
        if(ch!=' ')
            b[++cntB]=ch-'0';
    }
    scanf("%d%d",&E1,&E2);

    double res1=1,res2=1;
    for(int i=1;i<=cntA-1;i++)
        res1=res1*G[a[i]][a[i+1]];
    for(int i=1;i<=cntB-1;i++)
        res2=res2*G[b[i]][b[i+1]];


    double res3=0,res4=0;
    double temp=1;
    for(int i=1;i<=10000;i++){
        res3+=temp;
        temp=temp*G[E1][E1];
    }
    temp=1;
    for(int i=1;i<=10000;i++){
        res4+=temp;
        temp=temp*G[E2][E2];
    }

    printf("%.8lf\n",res1);
    printf("%.8lf\n",res2);
    printf("%.8lf\n",res3);
    printf("%.8lf\n",res4);

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值