ZOJ_3569_Dr. Zomboss's Revenge(概率期望)

来源:互联网 发布:用java输出九九乘法表 编辑:程序博客网 时间:2024/06/05 13:31

Dr. Zomboss's Revenge

Time Limit: 2 Seconds      Memory Limit: 65536 KB

These days MM is interested in the final stage of Plants vs Zombies, called "Dr. Zomboss's revenge".

In this stage, MM is provided with an empty map with n rows and m columns as well as n*m plants. MM is required to fill the map with these n*m plants, which come from t different kinds.

However, the terrible fact is that, Dr. Zomboss will randomly throw a fire ball to squash MM's precious plants. A fire ball will be randomly placed on any row, squashing an entire row of plants. For instance, for a map with 5 rows and 4 columns, the possibility of squashing the first row is 1/5.

MM has been accustomed to the inevitable squashing. What he cannot bear is that many plants of the same kind might be squashed at the same time. For each kind of plants, namely the ith (1 ≤ i ≤ t) kind, if the number of plants squashed is less or equal than bi, called the bearing factor, MM won't care. However, for every additional plant (compared to bi) with the ith kind squashed, MM will get his angry value increased by ai, called argry factor.

Now, MM needs to find a way to place his plants which can minimize the expectation of this total angry value and he asked you to tell him what the minimum expectation is.

Notice that the initial angry value of MM is 0.

Input

There are multiple cases. The first line of each case contains three integers n (1 ≤ n ≤ 10), m (1 ≤ m ≤ 10), t (1 ≤ t ≤ 30), as described above. 
Each of the following t lines contains three integers discribing a kind of plant: the number of plant ci (1 ≤ ci ≤ n*m), the bearing factor bi (1 ≤ bi ≤ ci)and the angry factor ai (1 ≤ ai ≤ 100). 
We assert that the sum of ci equals to n*m. Process to the end of file.

Output

For each test case, output the minimium mathmetical expectation in a single line, rounded to 3 digits after the decimal point.

Sample Input

2 3 23 1 13 2 2

Sample Output

0.500

Hint

we put plants like1 2 12 1 2If a fire ball is thrown in the first row, the angry value = 1If a fire ball is thrown in the second row, the angry value = 0Hence the expactation is 0.5


题型:概率期望


题意:

       植物大战僵尸。。。

       MM有n*m个植物种在n*m的地图里,僵尸会发出一个火球,火球会随机毁掉一行,也就是说每一行被毁掉的概率是 1/n。

       一共有k种植物,MM对每一种植物都有忍耐值和怒气值,比如对第i个植物,她的忍耐值为bi,怒气值为ai。当毁掉的一行时,设这一行上的 i th植物有ci个,若是ci<=bi,MM就不会管它,若是大于bi,怒气值就会上升(ci-bi)*ai。

       问怒气值的最小期望。


分析:

       期望就是每一行的怒气值期望的和,而火球毁掉哪一行的概率是相等的,都是 1 / n,所以只需要求出最小的总怒气值,最后除以n就可以了。

       为使怒气值最小,就需要将同种植物尽量分开来放,也就是尽量做到平均,所以可以看做每一行有 ci \ n 颗ci植物。最小的总怒气就是angry +=(ci / n - bi )*  a * n.

       所以总期望就是 sum += (ci / n - bi )*  a * n /  n =(ci / n - bi )*  a .

       但是一次一次的除会存在精度误差,所以可以将“ / n ”提到外面,即:

                                                   sum += (ci - bi * n )*  a; 

                                                   ans = sum / n.


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){    int n,m,t;    while(~scanf("%d %d %d",&n,&m,&t))    {        double num,b,a,sum=0;        for(int i=0;i<t;i++)        {            scanf("%lf%lf%lf",&num,&b,&a);            if((num-b*n)>0)                 sum+=(num-b*n)*a;        }        printf("%.3lf\n", sum/n);    }}


原创粉丝点击