CodeForces 261B Maxim and Restaurant [数学+基础DP]
来源:互联网 发布:网络宣传方式有哪些 编辑:程序博客网 时间:2024/05/16 23:41
题意(抽象后):给出N个占用空间为Ai的物体,现有空间大小P,对于物体的某个序列,从左至右依此塞到空间P中,直到塞到某一个Ai,空间容纳不下为止,此时P中含有i-1个物品。问对于物体的N!个序列,问最后放进去物体个数的期望。
范围:所有数小于50
解法:比较明显的背包模型,只是需要解决一个约束条件,即何时背包恰好装不下,这时可以枚举装不下的物体Ai,然后DP[I][J][K]表示前I个物体,选J个,总重量为K,但是不选Ai的方案数,这样就可以知晓恰好装到Ai却装不下的方案数。然后期望需要累加上 J*DP[N][J][K] * J!*(N-J-1)! / N! ,DP[N][J][K] 表示某个满足条件的状态;由于N!可能很大,需要边乘边除。
代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input char c; int sgn; T bit=0.1; if(c=getchar(),c==EOF) return 0; while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); if(c==' '||c=='\n'){ ret*=sgn; return 1; } while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10; ret*=sgn; return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)using namespace std;typedef long long ll;typedef pair<int,int> pii;ll dp[55][55][55];int a[55];int n,sum;double ans;int main(){ ans=0.0; scanff(n); int tot=0; rep(i,1,n)scanff(a[i]),tot+=a[i]; scanff(sum); if(tot<=sum){ printf("%.10f\n",n*1.0); return 0; } rep(c,1,n){ mem(dp,0); dp[0][0][0]=1; rep(i,1,n){ rep(j,0,i-1) rep(k,0,sum){ if(dp[i-1][j][k]){ dp[i][j][k]+=dp[i-1][j][k]; if(i!=c&&k+a[i]<=sum)dp[i][j+1][k+a[i]]+=dp[i-1][j][k]; } } } rep(j,1,n){ rep(k,max(0,sum-a[c]+1),50){ if(dp[n][j][k]){ double temp=dp[n][j][k]*j; int num=n-j; int n1=n-j,n2=n; rep(l,1,num){ temp*=double(n1); temp/=double(n2); n1--;n2--; } temp/=double(n-j); ans+=temp; } } } } printf("%.10f\n",ans); return 0;}
0 0
- CodeForces 261B Maxim and Restaurant [数学+基础DP]
- Codeforces Round #160 (Div. 1), problem: (B) Maxim and Restaurant DP+组合数学
- Codeforces 262D Maxim and Restaurant
- BNU28000 Maxim and Restaurant
- CodeForces 261BMaxim and Restaurant
- codeforces 261E Maxim and Calculator
- codeforces 261E Maxim and Calculator
- CodeForces 476B Dreamoon and WiFi 数学 概率 DP
- 【CodeForces】[597B]Restaurant
- Codeforces 610B Vika and Squares 【计数 数学基础】
- CodeForces 261A Maxim and Discounts(贪心)
- CodeForces 261A Maxim and Discounts [想法/贪心题]
- Codeforces 597B Restaurant 【贪心】
- Codeforces 854 B Maxim Buys an Apartment
- Codeforces 845 B. Maxim Buys an Apartment
- Codeforces 854B Maxim Buys an Apartment
- codeforces 377B Modulo Sum(数学,dp)
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp,组合数学
- CocoaPods的安装方法 安装ruby环境
- ios学习笔记(二)
- 1095. Cars on Campus
- java反射--Field
- 安装sql server时提示缺少.NET 3.5 sp1
- CodeForces 261B Maxim and Restaurant [数学+基础DP]
- JAVA----关键字用法
- iOS —— IOS8适配
- C#泛型与委托
- 前端不为人知的一面--前端冷知识集锦
- Lifecycle(生命周期)和Phase(阶段)
- jquery.autocomplete样本
- JQUERY实现判断鼠标往上滚往下滚的功能。
- PyQt4 学习笔记1基本框架