首页 > 代码库 > Peter's Hobby

Peter's Hobby

题目链接

  • 题意:
    题意比较麻烦。。。n天,给出每天的叶子的一种状态(Dry , Dryish , Damp and Soggy),最有可能出现的天气序列(Sunny, Cloudy and Rainy
    最开始,第一天处于每种状态有一个预定义的概率。每一天,根据当前的叶子状态,处于每种天气状况有一个给定的概率;题目中还给出任意两种天气状态的转移概率,即前一天处于某种天气时今天处于某种天气的概率。
  • 分析:
    就是简单的DP,每天处于每个点有一个固定的状态,前一天的每个状态的转移概率也是给定的,起始点也给定,所以就需要处理一下这些即可。除此之外,题目中需要输出“路径”,多记录一下即可
const int maxn = 100010;

double dp[maxn][3];
int ipt[maxn], p[maxn][3];
char s[10];
double d1[][4] =
{
    {0.6, 0.2, 0.15, 0.05},
    {0.25, 0.3, 0.2, 0.25},
    {0.05, 0.1, 0.35, 0.5}

};
double d2[][3] =
{
    {0.5, 0.375, 0.125},
    {0.25, 0.125, 0.625},
    {0.25, 0.375, 0.375}
};
map<string, int> mp;
char to[][10] = {"Sunny", "Cloudy", "Rainy"};

int main()
{
    mp["Dry"] = mp["Sunny"] = 0;
    mp["Dryish"] = mp["Cloudy"] = 1;
    mp["Damp"] = mp["Rainy"] = 2;
    mp["Soggy"] = 3;

    int T, n;
    RI(T);
    FE(kase, 1, T)
    {
        CLR(p, -1);
        RI(n);
        REP(i, n)
        {
            RS(s);
            ipt[i] = mp[s];
        }
        dp[0][0] = log(0.63) + log(d1[0][ipt[0]]);
        dp[0][1] = log(0.17) + log(d1[1][ipt[0]]);
        dp[0][2] = log(0.2) + log(d1[2][ipt[0]]);
        FF(i, 1, n)
        {
            REP(j, 3)
            {
                dp[i][j] = -1e10;
                REP(k, 3)
                {
                    double pre = dp[i - 1][k] + log(d2[k][j]) + log(d1[j][ipt[i]]);
                    if (pre > dp[i][j])
                    {
                        dp[i][j] = pre;
                        p[i][j] = k;
                    }
                }
            }
        }
        double Max = -1e10; int c = 0;
        REP(j, 3)
            if (dp[n - 1][j] > Max)
            {
                Max = dp[n - 1][j];
                c = j;
            }
        stack<int> sk; int r = n - 1;
        while (r >= 0)
        {
            sk.push(c);
            c = p[r--][c];
        }
        printf("Case #%d:\n", kase);
        while (!sk.empty())
        {
            printf("%s\n", to[sk.top()]);
            sk.pop();
        }
    }
    return 0;
}


Peter's Hobby