CF261B Maxim and Restaurant

我的blog

题目链接:CF261B Maxim and Restaurant

\[preface\]

背包DP+期望的一道题

\[description\]

题目翻译有QwQ

\[solution\]

我们要分两种情况讨论:

  • \[\sum_{i=1}^{n}a[i]\leq p\]
  • \[\sum_{i=1}^{n}a[i]> p\]

对于情况\(1\),显然就是n个人都可以坐,那么直接输出\(n\)即可。

对于请款\(2\),

设\(f(i,j,k)\)表示表示已经处理完前i个人选了j个人占了k这么长的方案数,那么答案显然是:
\[ans=\dfrac{\sum f(n,i,j)\times i!\times (n-i)!}{n!}\]

对于\(f(i,j,k)\)我们有两个策略,一个是选,一个是不选,得出:
\[f(i,j,k)=f(i-1,j-1,k-a[i])+f(i-1,j,k)\]
我们发现DP可以省掉一维

\[code\]

#include <cstdio>
#include <algorithm>
#define ll long long
#define re register
using namespace std;
template<typename T>
inline void read(T&x)
{
    x=0;
    char s=(char)getchar();
    bool f=false;
    while(!(s>='0'&&s<='9'))
    {
        if(s=='-')
            f=true;
        s=(char)getchar();
    }
    while(s>='0'&&s<='9')
    {
        x=(x<<1)+(x<<3)+s-'0';
        s=(char)getchar();
    }
    if(f)
        x=(~x)+1;
}
const int N=55;
int n,p,a[N],sum;
long long dp[N][N];
double fac[N]= {1.0},ans;
int main()
{
    read(n);
    for(re int i=1; i<=n; ++i)
    {
        read(a[i]);
        sum+=a[i];
        fac[i]=fac[i-1]*i;
    }
    read(p);
    if(sum<=p)
    {
        printf("%lf\n",(double)n);
        return 0;
    }
    dp[0][0]=1;
    for(re int i=1; i<=n; ++i)
        for(re int j=i; j>=1; --j)
            for(re int k=p; k>=a[i]; --k)
                dp[j][k]+=dp[j-1][k-a[i]];
    for(re int i=1; i<=n; ++i)
    {
        for(re int j=0; j<=p; ++j)
            printf("%lld ",dp[i][j]);
        putchar('\n');
    }
    for(re int i=1; i<=n; ++i)
        for(re int j=0; j<=p; ++j)
            ans+=(double)dp[i][j]*fac[i]*fac[n-i];
    printf("%lf\n",ans/fac[n]);
    return 0;
}

原文地址:https://www.cnblogs.com/wangjunrui/p/12512412.html

时间: 2024-10-16 22:41:30

CF261B Maxim and Restaurant的相关文章

codeforces 261B Maxim and Restaurant(概率DP)

B. Maxim and Restaurant time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has

cf 261B.Maxim and Restaurant

什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h> #define LL long long #define LD long double #define N 100005 using namespace std; inline int ra() { int x=0,f=1; char ch=getchar(); while (ch<'0' |

【CodeForces 261B】Maxim and Restaurant(DP,期望)

题目链接 第一种解法是$O(n^3*p)$的:f[i][j][k]表示前i个人进j个人长度为k有几种方案(排列固定为123..n时).$f[i][j][k]=f[i-1][j][k]+f[i-1][j-1][k-a[i]]$最外层枚举t表示被卡的那个人.i=t时不加上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!)$. 可以看看这篇题解 #include<cstdio> #

URAL1962:In Chinese Restaurant(并查集)

When Vova arrived in Guangzhou, his Chinese friends immediately invited him to a restaurant. Overalln people came to the restaurant, including Vova. The waiter offered to seat the whole company at a traditional large round table with a rotating dish

BestCoder Round #2 1001 TIANKENG’s restaurant

不得不说,bastcoder是个hack游戏啊!!! 题意:求最少要多少张椅子才能让所有来的客人有地方坐!! 就是一个区间的处理吧!!!和HDU  1556 我待水似流年,流年待我似水神似!!!! 求的是重叠的区间最大,我们只要标记每个区间会有多少人就可以了!!! 然后在从0到1440分统计就OK了!! AC代码如下: #include<iostream> #include<cstdio> #include<cmath> #include<map> #inc

hdu 4883 TIANKENG’s restaurant(暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come t

HDU 4883 TIANKENG’s restaurant Bestcoder 2-1(模拟)

TIANKENG's restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of t

HDU 4883 TIANKENG’s restaurant(区间选点)

HDU 4883 TIANKENG's restaurant 题目链接 题意:给定一些时间作为区间,和一个人数,问要安排多少人去看管(一个人只能看管一个人) 思路:普通的区间选点问题,一个区间拆成一个进入点一个出去点,然后排序循环求答案即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 20005; struct Man {

HDU ACM 1103 Flo&#39;s Restaurant

分析:借助STL的min_element实现.每次更新最先被占用的桌子,具体见注释. #include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C