传送门:http://www.ifrog.cc/acm/problem/1053
思路:
令dp[i][j]表示前ii个矿,得到jj个宝石,不会死亡的最大概率。
转移方程:
dp[i][j]=max(dp[i−1][j],dp[i−1][j−ai]∗(1−pi))
然后找不会死亡概率>0.19的最大宝石数即可。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rrep(i,k,n) for(int i=k;i>=n;i--)
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=1e5+10;
const double eps=1e-12;//double不能直接比较
int a[110];
double p[N], dp[110][50010];
int main(){
int T;
read(T);
while(T--){
int n, sum=0;
read(n);
rep(i, 1, n)read(a[i]), sum += a[i];
rep(i, 1, n)scanf("%lf", &p[i]);
mst(dp, 0);//memset可以给double类型数组赋值
rep(i, 0, n)dp[i][0] = 1.0;
rep(i, 1, n){
rep(j, 0, sum){
dp[i][j] = dp[i - 1][j];
if(j - a[i] >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]] * (1.0 - p[i]));
}
}
int ans;
rrep(j, sum, 1)if(dp[n][j] - 0.19 > eps){
ans = j;
break;
}
printf("%d\n", ans);
}
return 0;
}
描述:
Time Limit:1s Memory Limit:64MByte
Submissions:134Solved:42
A gemstone (also called a gem, fine gem, jewel, precious stone or semi-precious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli) or organic materials that are not minerals (such as amber or jet) are also used for jewelry and are therefore often considered to be gemstones as well. Most gemstones are hard, but some soft minerals are used in jewelry because of their luster or other physical properties that have aesthetic value. Rarity is another characteristic that lends value to a gemstone. Apart from jewelry, from earliest antiquity engraved gems and hardstone carvings, such as cups, were major luxury art forms. A gem maker is called a lapidary or gemcutter; a diamond worker is a diamantaire.You are a gemstone digger . There are nn mines. In each mine you will get a certain number of gems, but there is a certain probability that you will die.If your probability of death is≥0.81≥0.81 , you will be considered to must die. Ensuring that the probabilities are independent of each other. In the case you do not die, how many gemstones can you at most get?