AGU13-Save The Princess

2 篇文章 0 订阅

题目来源: http://www.codechef.com/AUG13/problems/SHIRO

题目描述:

Shiro is leading an army to save the princess of his kingdom "Abra". The princess has been made a prisoner by the neighboring kingdom "Kadabra". Kadabra is a magical land and is full of challenges. Shiro's army has to passN levels before they can get to the princess. After each level, the army gets a few flags to carry along. The flags can either all be of kindom Abra OR all of kingdom Kadabra. The magic of Kadabra forces Shiro's army to carry the flags irrespective of which kingdom they belong to. The princess doesn't know Shiro or anyone from his army. She will not escape with them unless she can trust them. She will trust them only if the number of Abra's flags they are carrying is atleast as much as the number of Kadabra's flags.

The army gets ai flags at the end of the ith level. Probability that flags received at the end of the ith level will be Abra's flags ispi. Your task is to tell Shiro what is the probability that the princess will trust him once they reach her.

Input:

First line of input contains a single integer T, the number of test cases.

Each test starts with a single line having an integer, N, the number of levels in Kadabra.

Next line contains contains N integers with the ith integer beingai as described above.

Next line contains contains N integers with the ith integer beingpi as described above. Note that the probabilities given will be in percents.

Output:

For each test case, output a line containing the required probability. The answer will be accepted if the relative error is not more than 10-6.

Constraints:

1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ ai ≤ 100
0 ≤ pi ≤ 100

Example:

Input:

2
5
1 2 3 4 4
0 100 100 0 50
2
5 5
50 60

Output:

0.5000000
0.8000000


题目大概意思:求Abra旗帜的个数大于等于总旗帜一半的概率,如第二组数据,一共有0,5,10,三种可能性,则p=0.5*0.6+0.5*0.4+0.5*0.6=0.8;

所以可以有一个递归式 Pro[j]=Pro[j]*(1-p[i])+Pro[j-a[i]]*p[i]   也就是DP。

最后求Pro[sum/2]~Pro[sum]的和


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 10010
using namespace std;
double Pro[MAX];
int a[110];
double p[110];
int N;
int sum=0;
void input()
{
   scanf("%d",&N);
   for(int i=1;i<=N;i++)
   {
       scanf("%d",&a[i]);
       sum+=a[i];
   }
   for(int i=1;i<=N;i++)
   {
       int c;
       scanf("%d",&c);
       p[i]=(double)c/100;
   }
}
void solve()
{
   for(int i=1;i<=N;i++)
   {
       for(int j=MAX;j>=a[i];j--)
       {
           Pro[j]=Pro[j]*(1-p[i])+Pro[j-a[i]]*p[i];
       }
       for(int k=0;k<a[i];k++)
         Pro[k]=Pro[k]*(1-p[i]);
   }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(Pro,0,sizeof(Pro));
        Pro[0]=1;
        input();
        solve();
        double ans=0;
        for(int i=((sum+1)/2);i<=sum;i++)
          {
              //printf("%.5f\n",Pro[i]);
              ans+=Pro[i];
          }
        printf("%.7f\n",ans);
        sum=0;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值