"Codeforces 261B" Maxim and Restaurant (DP, expected)

Source: Internet
Author: User

Topic links

The first solution is $o (n^3*p) $:
F[i][j][k] indicates that the first I person into J individual length of K has several schemes (arranged fixed to 123). N).
$f [i][j][k]=f[i-1][j][k]+f[i-1][j-1][k-a[i]]$
The outermost enumeration T represents the person being stuck. I=t not add f[i-1][j-1][k-a[i]].
$ans ={{(\sum f[n][j][k]*j*j!* (N-1-J)!) + (\sum f[n][n][k]*n)}}/(n!) $

< Span class= "Mjx-char mjxc-tex-main-r" >< Span class= "Mjx-char mjxc-tex-math-i" > < Span class= "MJX_ASSISTIVE_MATHML" >
to see this

#include <cstdio> #include <cstring> #define N 55int n,p,a[n];d ouble f[n][n][n],ans,c[n]= {1};int main () {    scanf ("%d", &n);    for (int i=1; i<=n; i++) {        scanf ("%d", &a[i]);        c[i]=c[i-1]*i;    }    scanf ("%d", &p);    for (int t=0; t<=n; t++) {        memset (f,0,sizeof f);        F[0][0][0]=1;        for (int i=1, i<=n; i++) for            (int j=0; j<=i; j + +) for                (int k=0; k<=p; k++) {                    f[i][j][k]=f[i-1][j][k ];                    if (J>=1&&t!=i&&k>=a[i]) f[i][j][k]+=f[i-1][j-1][k-a[i]];                }        for (int j=1; j<n; j + +) for            (int k=1; k<=p; k++) if (a[t]+k>p)                ans+=c[j]*c[n-1-j]/c[n]*f[n][j][k]*j;        for (int k=1; k<=p; k++) ans+=f[n][n][k]*n;    }    printf ("%lf", ans);}

can also be more efficient, $O (N^2*P) $

F[i][j][k] represents the first I person at least into J personal This J person's length and for K has several schemes (ranked as 123). N).
So $ans= (\sum f[n][j][k]*j!* (n-j)!/(n!) $

I didn't quite understand why at least J was such a push, asked the next teammate, said because there is no card behind, so it is possible to enter more people.
In fact, the first method of F is also at least J person, and then add ans in the back when the third j+1 person.

And you can use a scrolling array to optimize to a 2-D array.

#include <cstdio> #define N 51int n,a[n],p;double fac[n]= {1},f[n][n],tol,ans;int main () {    scanf ("%d", &n) ;    for (int i=1; i<=n; i++)    {        scanf ("%d", a+i);        Fac[i]=fac[i-1]*i;        Tol+=a[i];    }    scanf ("%d", &p);    if (tol<=p)    {        printf ("%d", n);        return 0;    }    F[0][0]=1;    for (int i=1, i<=n; i++) for        (int. j=n-1; j>=0; j--) for            (int k=0; k+a[i]<=p; k++)                f[j+1][k+a[i]]+= F[J][K];    for (int j=1; j<=n; j + +) for        (int k=0; k<=p; k++)            ans+=f[j][k]*fac[j]*fac[n-j];    Ans/=fac[n];    printf ("%lf", ans);}


"Codeforces 261B" Maxim and Restaurant (DP, expected)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.