经典的概率DP。关键是每次开门只能进/出一个人。
Give Me Your Hand Time Limit: 2 Seconds Memory Limit: 131072 KB Special JudgeBellyWhite and MightyHorse are two students of Marjar University. They are best friends and also roommates living in the same dorm. There are K students living in that dorm, including BellyWhite and MightyHorse.
BellyWhite posted a strange microblog in 2012-9-3:
This is quite strange. It means that BellyWhite will chop some "hands" into pieces if he's been found playing games or some other activities which is not relevant to his research. This logic is insane, isn't it?
We could call those things which are not relevant to BellyWhite's research "bad things". Now we know that BellyWhite will start doing bad things when he's in the dorm for T minutes, and he will stop doing those things when he's being warned by MightyHorse or leaving the dorm. If he's been warned to stop doing bad things and then he stays in the dorm for another T minutes, he will start doing bad things again.
MightyHorse noticed the strange microblog BellyWhite posted, but he's a good roommate, so he must took the risk of losing his "hands" to warn BellyWhite that he was doing something harmful to his research progress or even his PhD degree. Fortunately, MightyHorse has M plastic toy "hands", so BellyWhite will only chop those toy hands into pieces when he's being warned.
Here comes the problem. We only know that no one is in the dorm initially, and we heard N door open and close sounds, which means there are N people entered or exited the dorm. We logged exact time of all door sounds, but we don't know who made the sound, that means all K students living in the dorm have the same possibility to enter / exit. We'd like to know the expected number of toy hands MightyHorse will have after 24 hours (1440 minutes).
Please note that using toy hand to stop BellyWhite from doing bad things take no time, which means that even at the moment MightyHorse or BellyWhite enter / exit the dorm, a toy hand will be used to stop the bad thing. But if that there's no toy hand left, MightyHorse will not able to stop BellyWhite from doing bad things.
There are multiple test cases. The first line of input is an integer Casenum indicating the number of test cases.
For each test case, the first line contains 4 integers T (1 ≤ T ≤ 100), N (1 ≤ N ≤ 100), M (1 ≤ M ≤ 100) andK (2 ≤ K ≤ 8) which are defined in descriptions above.
The following line contains N integers. For every integer ti(0 ≤ ti ≤ 1440, 0 ≤ i < N), it means that a student entered or exited the dorm at time ti.
We guarantee that all those N ti will be given in strictly increasing order.
2 60 2 10 2 200 260 100 2 8 5 1340 1341
5.000000 7.960000
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int n,m,T,K; ///N次开门,M个手,T分钟就干坏事,K个人 double dp[111][5][114][111]; ///dp[第几次][位置关系][被警告一次后在屋内待了多久][剩下几只手]=概率 int t[122]; int main() { int ca,i,j,k; cin>>ca; while(ca--) { memset(dp,0,sizeof dp); cin>>T>>n>>m>>K; dp[0][0][0][m]=1; t[0]=0; for(int i=1;i<=n;i++)cin>>t[i]; for(int i=1;i<=n;i++) { ///dp[第几次][位置关系][被警告一次后在屋内待了多久][剩下几只手]=概率 int ti=t[i]-t[i-1]; ///BellyWhite犯错误砍手 int k=K; for(int j=0;j<=m;j++)///State 0:BellyWhite屋外 MightyHorse屋外 { dp[i][0][0][j]+=dp[i-1][0][0][j]*(k-2)/k; dp[i][1][0][j]+=dp[i-1][0][0][j]*1./k; dp[i][2][0][j]+=dp[i-1][0][0][j]*1./k; } for(int j=0;j<=m;j++)///State 1:BellyWhite屋外 MightyHorse屋内 { dp[i][0][0][j]+=dp[i-1][1][0][j]*1/k; dp[i][1][0][j]+=dp[i-1][1][0][j]*(k-2)/k; dp[i][3][0][j]+=dp[i-1][1][0][j]*1/k; } for(k=0;k<=T;k++) for(int j=0;j<=m;j++)///State 2:BellyWhite屋内 MightyHorse屋外 { dp[i][3][(k+ti)>=T?0:(k+ti)][max(0,j-(k+ti>=T))]+=dp[i-1][2][k][j]/K; dp[i][0][0][j]+=dp[i-1][2][k][j]/K; dp[i][2][min(T,k+ti)][j]+=dp[i-1][2][k][j]*(K-2)/K; } for(int k=0;k<=T;k++) for(int j=0;j<=m;j++)///State 3:BellyWhite屋内 MightyHorse屋内 { int a,b; a=(k+ti)%T,b=max(0,j-(k+ti)/T); dp[i][3][a][b]+=dp[i-1][3][k][j]*(K-2)/K; dp[i][2][a][b]+=dp[i-1][3][k][j]/K; dp[i][1][0][b]+=dp[i-1][3][k][j]/K; } } int tmp=1440-t[n]; double ans=0; for(int i=0;i<=2;i++) for(int j=0;j<=T;j++) for(int k=0;k<=m;k++) ans+=dp[n][i][j][k]*k; for(int i=0;i<=T;i++) for(int j=0;j<=m;j++) ans+=dp[n][3][i][j]*max(0,j-(i+tmp)/T); printf("%.6f\n",ans); } }