Http://codeforces.com/problemset/problem/261/B
Title: given n number a1...an (n<=50,ai<=50), randomly disturbed, remember Si=a1+a2+a3...+ai, ask to meet the maximum value of si<=p I expectations. (P<=50)
(to the effect from http://www.cnblogs.com/liu-runda/p/6253569.html)
We know that the whole arrangement is actually equivalent to the probability of inserting a value into a sequence one by one.
So we can look at the problem this way:
? Now there are n number, n boxes, and so on the probability to put these numbers into the box, seeking Si<=p's max[i] expectations
We consider each box's contribution to the answer.
If the first box is saved, then its contribution is P (the probability of saving)
So we just have to calculate the probability that each box will be saved.
We also found that if the first box was preserved, then the 1~ (i-1) box must have been preserved.
Otherwise, the first box can not be preserved, in the original text:
Maxim doesn ' t let no other guest in the restaurant
Even if one of the following guests in the queue would has fit in the table.
Plays a key role
So we define F[I][J][K] to indicate that the number of the first I is randomly placed in the box
The probability that the sum of the number of the first J box is filled and the first J box is K
Then we have f[i][j][k] = f[i-1][j][k]* (1-(j/i)) + f[i-1][j-1][k-a[i]]* (j/i)
Then ans = sigma{f[n][i][j]} (1<=I<=N;0<=J<=P)
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefLong Longll;6InlinevoidReadint&x) {7x=0;CharChBOOLFlag =false;8 while(Ch=getchar (),ch<'!');if(ch = ='-') Ch=getchar (), flag =true;9 while(x=Ten*x+ch-'0', Ch=getchar (),ch>'!');if(flag) x=-x;Ten } OneInlineintCat_max (Const int&a,Const int&B) {returnA>b?a:b;} AInlineintCat_min (Const int&a,Const int&B) {returnA<b?a:b;} - Const intMAXN = the; - Doublef[2][MAXN][MAXN]; the intA[MAXN]; - intMain () { - intn;read (n); - for(intI=1; i<=n;++i) read (A[i]); + intP;read (p); -f[0][0][0] =1.0; + for(intI=1; i<=n;++i) { A for(intj=0; j<=i;++j) { at for(intk=0; k<=p;++k) { - if(A[i] > K) f[i&1][j][k] = f[(i-1) &1][j][k]* (I-J)/i; - Elsef[i&1][j][k] = f[(i-1) &1][j][k]* (I-J)/i + f[(i-1) &1][j-1][k-a[i]]*j/i; - } - } - } in DoubleAns =.0; - for(intI=1; i<=n;++i) { to for(intj=0; j<=p;++j) { +Ans + = f[n&1][i][j]; - } the}printf ("%.10lf\n", ans); * GetChar (); GetChar (); $ return 0;Panax Notoginseng } -
codeforces-261b Maxim and Restaurant