hdu 4865 Peter's Hobby(2014 多校联合第一场 E)

Peter‘s Hobby

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 545    Accepted Submission(s): 237

Problem Description

Recently, Peter likes to measure the humidity of leaves. He recorded a leaf humidity every day. There are four types of leaves wetness: Dry , Dryish , Damp and Soggy. As we know, the humidity of leaves is affected by the weather. And there are only three kinds
of weather: Sunny, Cloudy and Rainy.For example, under Sunny conditions, the possibility of leaves are dry is 0.6.

Give you the possibility list of weather to the humidity of leaves.

The weather today is affected by the weather yesterday. For example, if yesterday is Sunny, the possibility of today cloudy is 0.375.

The relationship between weather today and weather yesterday is following by table:

Now,Peter has some recodes of the humidity of leaves in N days.And we know the weather conditons on the first day : the probability of sunny is 0.63,the probability of cloudy is 0.17,the probability of rainny is 0.2.Could you know the weathers of these days
most probably like in order?

http://acm.hdu.edu.cn/showproblem.php?pid=4865

题目大意:有三种天气和四种湿度,给出在每种天气下四种湿度的概率,和天气之间的转移概率,现在给出一个n天的湿度序列,要求给出一个概率最大的天气序列。

思路:很简单的概率DP,转移概率和状态概率都给好了,设dp[i][j]表示第i天是第j种天气的最大概率,然后pre[i][j]来记录序列顺序。

假设第i-1天是第j1种天气,第i天是j2种天气,天气转移概率为p1[j1][j2],第i天的湿度为x,在第j2种天气下x湿度的概率为p2[j2][x],则有:

dp[i][j2]=max(dp[i-1][j1]*p1[j1][j2]*p2[j2][x]),记录最大值是由哪一个j1转移过来的,用pre[i][j2]记录。最后求得最大的dp[n][j],根据pre数组输出路径即可。

注:题目中说由于dp[n][j]可能很小,用double乘可能会掉精度,所以要用log,但是我直接乘也过了,可能是运气比较好吧,以后这种问题还是要多注意。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <vector>
#define maxn 55
using namespace std;
double dp[55][3];
int pre[55][3];
map<string,int> mp;
string str;
double p1[3][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 p2[3][3]={0.5,0.375,0.125,0.25,0.125,0.625,0.25,0.375,0.375};
void init()
{
    mp.insert(make_pair("Dry",0));
    mp.insert(make_pair("Dryish",1));
    mp.insert(make_pair("Damp",2));
    mp.insert(make_pair("Soggy",3));
}
int main()
{
    int ncase,T=0;
    scanf("%d",&ncase);
    init();
    while(ncase--)
    {
        printf("Case #%d:\n",++T);
        int n;
        scanf("%d",&n);
        cin>>str;
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<3;j++)
            dp[i][j]=0;
        }
        int lab=mp[str];
        memset(pre,0,sizeof(pre));
        dp[1][0]=0.63*p1[0][lab];
        dp[1][1]=0.17*p1[1][lab];
        dp[1][2]=0.2*p1[2][lab];
        for(int i=2;i<=n;i++)
        {
            cin>>str;
            int lab=mp[str];
            for(int j=0;j<3;j++)
            {
                for(int k=0;k<3;k++)
                {
                    double pp=dp[i-1][k]*p2[k][j]*p1[j][lab];
                    if(pp>dp[i][j])
                    {
                        dp[i][j]=pp;
                        pre[i][j]=k;
                    }
                }
            }
        }
        vector<int> ans;
        double mi=0;
        int po;
        for(int i=0;i<3;i++)
        {
            if(dp[n][i]>mi)
            {
                mi=dp[n][i];
                po=i;
            }
        }
        ans.push_back(po);
        int now=n;
        while(now!=1)
        {
            po=pre[now][po];
            ans.push_back(po);
            now--;
        }
        for(int i=n-1;i>=0;i--)
        {
            if(ans[i]==0)
            printf("Sunny\n");
            else if(ans[i]==1)
            printf("Cloudy\n");
            else
            printf("Rainy\n");
        }
    }
    return 0;
}

hdu 4865 Peter's Hobby(2014 多校联合第一场 E)

时间: 2024-11-08 18:51:41

hdu 4865 Peter's Hobby(2014 多校联合第一场 E)的相关文章

hdu 4865 Peter&#39;s Hobby(2014 多校联合第一场 E)

题意:已知昨天天气与今天天气状况的概率关系(wePro),和今天天气状态和叶子湿度的概率关系(lePro)第一天为sunny 概率为 0.63,cloudy 概率 0.17,rainny 概率 0.2.给定n天的叶子湿度状态,求这n天最可能的天气情况 分析:概率dp设 dp[i][j] 表示第i天天气为j的最大概率,pre[i][j]表示第i天天气最可能为j的前一天天气,dp[i][j]=max(dp[i-1][k]+log(wePro[k][j])+log(lePro[j][lePos[i]]

HDU 4869 Turn the pokers (2014 多校联合第一场 I)

HDOJ--4869--Turn the pokers[组合数学+快速幂] 题意:有m张扑克,开始时全部正面朝下,你可以翻n次牌,每次可以翻xi张,翻拍规则就是正面朝下变背面朝下,反之亦然,问经过n次翻牌后牌的朝向有多少种情况.我们可以把正面朝上理解为1,反面朝上理解为0,那么可以理解为求01串的不同的组合方式有几种. 解题思路:我们可以知道,每张牌假设起始状态都为0,如果翻奇数次,该牌最后的情况是1,如果翻偶数次,该牌的最后情况为0.根据n次翻牌的个数找出1的个数的下限和上限,然后再在这个范围

hdu 4865 Peter&amp;#39;s Hobby(2014 多校联合第一场 E)

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 545    Accepted Submission(s): 237 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

hdu 4869 Turn the pokers (2014多校联合第一场 I)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1265    Accepted Submission(s): 465 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

2014多校联合第一场

1001:Couple doubi 暴力打表找规律可知,对于任意的p. (1^i+2^i+...+(p-1)^i)%p={ 非0     ,i%(p-1)==0 0        ,  i%(p-1)!=0 } 所以,结果就很显然了. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> #include<c

HDU 4870 Rating (2014 多校联合第一场 J)(概率)

题意: 一个人有两个TC的账号,一开始两个账号rating都是0,然后每次它会选择里面rating较小的一个账号去打比赛,每次比赛有p的概率+1分,有1-p的概率-2分,当然如果本身是<=2分的也就还是回到0分.然后问最后其中一个账号到达20分时需要打多少次比赛. 思路: 因为每次50分,到达1000分,所以可以看做每次1分,到达20分dp[i]表示i到20的数学期望那么dp[i] = dp[i+1]*p+dp[i-2]*q+1;令t[i] = dp[i+1]-dp[i]则t[i] = (t[i

2014多校第一场 E 题 || HDU 4865 Peter&#39;s Hobby (DP)

题目链接 题意 : 给你两个表格,第一个表格是三种天气下出现四种湿度的可能性.第二个表格是,昨天出现的三种天气下,今天出现三种天气的可能性.然后给你这几天的湿度,告诉你第一天出现三种天气的可能性,让你求出最可能出现的天气序列 . 思路 : 定义第 i 天叶子湿度为hum[i].第 i 天,天气为 j 的最大概率为dp[i][j].wealea[i][j]表示天气为 i 叶子为j的概率,weawea[i][j]表示今天天气为 i 明天天气为j的概率,st[i]表示第一天天气为i的概率.pre[i]

HDU 4865 Peter&#39;s Hobby

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 647    Accepted Submission(s): 273 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

HDU 4865 Peter&#39;s Hobby --概率DP

题意:第i天的天气会一定概率地影响第i+1天的天气,也会一定概率地影响这一天的湿度.概率在表中给出.给出n天的湿度,推测概率最大的这n天的天气. 分析:这是引自机器学习中隐马尔科夫模型的入门模型,其实在这里直接DP就可以了 定义:dp[i][j]为第i天天气为j(0,1,2分别表示三个天气)的概率,path[i][j]记录路径,path[i][j] = k 意思是前一天天气为k时,这一天有最大的概率是天气j. 做一个三重循环,对于每天,枚举今天的天气,再在里面枚举昨天的天气,则有: dp[i][