您的位置:首页 > 其它

2014多校联合训练1 E.Peter's Hobby

2014-07-24 10:06 323 查看

Peter's Hobby

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

Total Submission(s): 297 Accepted Submission(s): 42

[align=left]Problem Description[/align]

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?


[align=left]Input[/align]

The first line is T, means the number of cases, then the followings are T cases. for each case:
The first line is a integer n(n<=50),means the number of days, and the next n lines, each line is a string shows the humidity  of leaves (Dry, Dryish, Damp, Soggy)


[align=left]Output[/align]

For each test case, print the case number on its own line. Then is the most possible weather sequence.( We guarantee that the data has a unique solution)


[align=left]Sample Input[/align]

1
3
Dry
Damp
Soggy


[align=left]Sample Output[/align]

Case #1:
Sunny
Cloudy
Rainy

HintLog is useful.

先求出叶子呈现某个状态的时候是哪一种天气的概率,即比如呈现A状态,三种天气下产生A状态的概率为a,b,c,设sum=a+b+c,我们则要求出a/sum,b/sum,c/sum.
然后设dp[i][j]为第i天天气是j的最大概率,可以由第i-1天的k天气的最大概率dp[i-1][k]乘以今天j天气的概率q[k][j],再由今天j天气出现i状态的概率p[a[i]][j],枚举k的3个值,取最大值,并记录下最可能使得第i天呈现j天气的前一天的天气k。

[code]sum=dp[i-1][k]*q[k][j]*p[a[i]][j];
if(sum>dp[i][j])dp[i][j]=sum,rec[i][j]=k;

到最后一天的时候,我们找出最后一天最可能呈现的天气,记录下来,然后根据我们之前记录的使得最后一天的天气这样呈现前一天的天气,再向前寻找,然后就可以找到全部的天气序列。反向打印即可

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#define pi acos(-1.0)
#define eps 1e-8
#define ll long long
#define L 1000050
#define N 105
#define Mod 1000000007
#define M 99999999999
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int main(){
map<string,int>b;
b["Dry"]=0;
b["Dryish"]=1;
b["Damp"]=2;
b["Soggy"]=3;

map<int,string>c;
c[0]="Sunny";
c[1]="Cloudy";
c[2]="Rainy";

double p[4][3]={
0.6,0.25,0.05,
0.2,0.3,0.10,
0.15,0.2,0.35,
0.05,0.25,0.50,
};
double q[3][3]={
0.5,0.375,0.125,
0.25,0.125,0.625,
0.25,0.375,0.375,
};
int n,tag,i,j,k,a[N],num;
double tmp,sum,pro,dp[N][3];
string x;
int rec[N][3];
for(i=0;i<4;i++){
sum=p[i][0]+p[i][1]+p[i][2];
for(j=0;j<3;j++)p[i][j]=p[i][j]/sum;
}
int t,cas=1;
int ans[N];

for(scanf("%d",&t);t--;){
memset(ans,0,sizeof(ans));
scanf("%d",&n);
memset(dp,0,sizeof(dp));
dp[0][0]=0.63;dp[0][1]=0.17;dp[0][2]=0.2;
for(i=0;i<n;i++){cin>>x;a[i]=b[x];}

for(i=1;i<n;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
sum=dp[i-1][k]*q[k][j]*p[a[i]][j];
if(sum>dp[i][j])dp[i][j]=sum,rec[i][j]=k;
}
}
}
tmp=0;
for(i=0;i<3;i++)
if(tmp<dp[n-1][i])tmp=dp[n-1][i],tag=i;

printf("Case #%d:\n",cas++);
num=0;
for(i=n-1;i>=0;i--){
ans[num++]=tag;
tag=rec[i][tag];
}
for(i=num-1;i>=0;i--)
cout<<c[ans[i]]<<"\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: