hdoj 5570 balls 【概率dp 求期望】
2015-11-28 11:45
344 查看
ballsTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 155 Accepted Submission(s): 104 Problem Description There are n balls with m colors. The possibility of that the color of the i-th ball is color j is ai,jai,1+ai,2+...+ai,m. If the number of balls with the j-th is x, then you should pay x2 as the cost. Please calculate the expectation of the cost. Input Several test cases(about 5) For each cases, first come 2 integers, n,m(1≤n≤1000,1≤m≤1000) Then follows n lines with m numbers ai,j(1≤ai≤100) Output For each cases, please output the answer with two decimal places. Sample Input 2 2 1 1 3 5 2 2 4 5 4 2 2 2 2 4 1 4 Sample Output 3.00 2.96 3.20 |
数组下标是从0开始的。
思路:设置状态X[i][j],若第i个球颜色为j则X[i][j] = 1,反之x[i][j] = 0。用C[j]表示颜色为j的球的个数。
则有C[j] = x[0][j] + x[1][j] + x[2][j] + ... + x[n-1][j]。
E(cost) = (0<=j<m)sigma(E(C[j]^2)) = (0<=j<m)sigma(E(x[0][j] + x[1][j] + ... + x[n-1][j])^2)。
注公式(一):2ab+2bc+2ac + a+b+c = (a+b+c)^2 - (a^2+b^2+c^2) + (a+b+c) -> n个变量同样适用。
由公式一得到(x[0][j]+...+x[n-1][j])^2表达式
E(cost) = (0<=j<m)sigma(E((x[0][j]^2+...+x[n-1][j]^2) + (2*x[b][j]*x[c][j])), 0<=b, c<n&&b!=c)。
由于期望的线性性质我们把上面的式子拆开:
E(cost) = (0<=j<m)sigma(E(x[k][j]^2), 0<=k<n) + sigma(E(2*x[b][j]*x[c][j]), 0<=b, c<n&&b!=c)。
通过计算可以得到 注:p[i][j]表示第i个球为颜色j的概率。
一、任意(0<=k<n)E(x[k][j]^2) = p[k][j]。
二、任意(0<=b, c<n&&b!=c)E(x[b][j]*x[c][j]) = p[b][j] * p[c][j]。
最终表达式E(cost) = (0<=j<m)sigma(p[k][j], 0<=k<n) + sigma(p[b][j]*p[c][j], 0<=b, c<n&&b!=c)。
注意:p[b][j]*p[c][j],p[c][j]*p[b][j] 都是存在的。
这样
E(cost) = (0<=j<m)sigma(p[k][j], 0<=k<n) + sigma(2*p[b][j]*p[c][j], 0 <= b < c < n)。
由公式一化简得到
E(cost) = (0<=j<m)[sigma(p[k][j])^2 - sigma(p[k][j]^2) + sigma(p[k][j]), 0<=k<n]。
这样时间复杂度O(nm)。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f #define eps 1e-8 #define MAXN (1000+10) #define MAXM (100000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; double a[MAXN][MAXN]; double p[MAXN][MAXN]; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 0; i < n; i++) { double sum = 0; for(int j = 0; j < m; j++) { Rf(a[i][j]); sum += a[i][j]; } for(int j = 0; j < m; j++) p[i][j] = a[i][j] / sum; } double ans = 0; for(int j = 0; j < m; j++) { double Sq = 0, S = 0; for(int i = 0; i < n; i++) { Sq += p[i][j] * p[i][j]; S += p[i][j]; } ans += S * S - Sq + S; } Pf(ans); } return 0; }
相关文章推荐
- hdoj 5568 sequence2 【dp + 高精度】
- iOS 【应用程序app运行原理-UIApplication-AppDelegate.m-UIApplicationMain】
- 【Ajax 3】JavaScript封装Ajax
- jQuery改造插件,添加回调函数
- 求连续操作(登录)数量(次数)最大的记录(用户)
- hibernate继承关系映射关系方法(二)--每个子类一张表
- TextView限制最大行数,多出部分显示省略号,解决ellipsize = "end" 失效的问题
- hdoj 5569 matrix 【裸dp】
- hdoj 5567 sequence1 【水题】
- python抓取gb2312/gbk编码网页乱码问题
- nyoj 17 单调递增最长子序列 【dp】
- Java学习日记-2.1 运算符
- 第二十八节 在PC端实现OAD空中升级
- bzoj2565 最长双回文串 manacher
- VS2013的error LNK2038 RuntimeLibrary 不匹配的解决
- ant 初步
- python培训day5 随笔
- nyoj 127 星际之门(一)【最小生成树】
- bzoj2152 聪聪可可 点分治
- 本地调试微信接口方法