ZOJ 3711 Give Me Your Hand

Give Me Your Hand

Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge

BellyWhite 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.

Input

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) and K (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.

Output

For each test case, output the expected number of toy hands that MightyHorse still have at time 1440, rounded to 6 decimal points.

Sample Input

260 2 10 2200 260100 2 8 51340 1341

Sample Output

5.0000007.960000


Author: FAN, Yuzhe
Contest: The 10th Zhejiang Provincial Collegiate Programming Contest 


 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 double dp[110][4][110][110];
 8 ///dp[第几次][位置关系][被警告一次后再屋内待了多久][剩下几只手]=概率
 9 int t[110];
10 
11 int ca,T,N,M,K;
12 
13 int main()
14 {
15     scanf("%d",&ca);
16     while(ca--)
17     {
18         scanf("%d%d%d%d",&T,&N,&M,&K);
19         memset(dp,0,sizeof(dp));
20         dp[0][0][0][M]=1.0;t[0]=0;
21         for(int i=1;i<=N;i++)
22         {
23             scanf("%d",&t[i]);
24             int ti=t[i]-t[i-1];
25             ///state 0: b  X   m  X
26             for(int j=0;j<=M;j++)
27             {
28                 dp[i][0][0][j]+=dp[i-1][0][0][j]*(K-2.)*1./K;
29                 dp[i][1][0][j]+=dp[i-1][0][0][j]*1./K;
30                 dp[i][2][0][j]+=dp[i-1][0][0][j]*1./K;
31             }
32             ///state 1: b X m Y
33             for(int j=0;j<=M;j++)
34             {
35                 dp[i][0][0][j]+=dp[i-1][1][0][j]*1./K;
36                 dp[i][1][0][j]+=dp[i-1][1][0][j]*(K-2.)*1./K;
37                 dp[i][3][0][j]+=dp[i-1][1][0][j]*1./K;
38             }
39             ///state 2 b Y  m X
40             for(int k=0;k<=T;k++)
41             {
42                 for(int j=0;j<=M;j++)
43                 {
44                     dp[i][0][0][j]+=dp[i-1][2][k][j]*1./K;
45                     dp[i][2][min(T,k+ti)][j]+=dp[i-1][2][k][j]*(K-2.)*1./K;
46                     dp[i][3][(k+ti)>=T?0:(k+ti)][max(0,j-((k+ti)>=T))]+=dp[i-1][2][k][j]*1./K;
47                 }
48             }
49             ///state 3 b Y m Y
50             for(int k=0;k<=T;k++)
51             {
52                 for(int j=0;j<=M;j++)
53                 {
54                     dp[i][1][0][max(0,j-(k+ti)/T)]+=dp[i-1][3][k][j]*1./K;
55                     dp[i][2][(k+ti)%T][max(0,j-(k+ti)/T)]+=dp[i-1][3][k][j]*1./K;
56                     dp[i][3][(k+ti)%T][max(0,j-(k+ti)/T)]+=dp[i-1][3][k][j]*(K-2.)/K;
57                 }
58             }
59         }
60         double ans=0.;
61         for(int i=0;i<=T;i++)
62         {
63             for(int j=0;j<=2;j++)
64             {
65                 for(int k=0;k<=M;k++)
66                 {
67                     ans+=dp[N][j][i][k]*k;
68                 }
69             }
70         }
71         int ti=1440-t[N];
72         for(int i=0;i<=T;i++)
73         {
74             for(int j=0;j<=M;j++)
75             {
76                 ans+=dp[N][3][i][j]*max(0,j-(i+ti)/T);
77             }
78         }
79         printf("%.6lf\n",ans);
80     }
81     return 0;
82 }

 

 

posted @ 2013-10-31 12:56  码代码的猿猿  阅读(454)  评论(0编辑  收藏  举报