• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codechef August Challenge 2013 Save The Princess

Save The Princess

Problem code: SHIRO

  • My Submissions
  • All Submissions
dp[j]表示前i个A旗子数量大于等于j的概率!
当j-a[i]<=0时0+a[i]肯定大于等于j所以dp[j-a[i]]=1;
然后倒着dp就不用开2维数组了,因为后面的只可能它以及前面的,对每个i从后面修改起!
把dp[j]看成前i个A旗子的数量为j的概率也行,就初始化,改一下,和结尾改成求和还有j-a[i]<0时0+a[i]!=j所以dp为0就不用加那一部分!

All submissions for this problem are available.

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 pass N 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 is pi. 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 being ai as described above.
Next line contains contains N integers with the ith integer being pi 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


Update:

Difference in answer upto 1e-6 will be ignored.

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <vector>
 4 #include <stack>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 #define maxn 10005
 9 #define INF 0x7fffffff
10 int n, m, k, x, y, d, t,ans;
11 int a[maxn];
12 double b[maxn], dp[maxn];
13 int main(){
14     scanf("%d", &t);
15     while (t--){
16         memset(dp, 0, sizeof dp);
17         scanf("%d", &n);
18         int s = 0;
19         for (int i = 1; i <= n; i++)scanf("%d", &a[i]), s += a[i];
20         for (int i = 1; i <= n; i++)scanf("%lf", &b[i]), b[i]/=100;
21         for (int i = 1; i <= n; i++)
22             for (int j =s-s/2; j >= 0 ; j--)
23                 if (j - a[i] > 0)dp[j] = dp[j] * (1 - b[i]) + dp[j - a[i]] * b[i];
24                 else dp[j] = dp[j] * (1 - b[i])+b[i];
25         printf("%.7f\n", dp[s-s/2]);
26     }
27     return 0;    
28 }
View Code

 

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <vector>
 4 #include <stack>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 #define maxn 10005
 9 #define INF 0x7fffffff
10 int n, m, k, x, y, d, t,ans;
11 int a[maxn];
12 double b[maxn], dp[maxn];
13 int main(){
14     scanf("%d", &t);
15     while (t--){
16         memset(dp, 0, sizeof dp);
17         scanf("%d", &n);
18         int s = 0;
19         for (int i = 1; i <= n; i++)scanf("%d", &a[i]), s += a[i];
20         for (int i = 1; i <= n; i++)scanf("%lf", &b[i]), b[i]/=100;
21         dp[0] = 1;
22         for (int i = 1; i <= n; i++)
23             for (int j =s ; j >= 0 ; j--)
24                 if (j - a[i] >= 0)dp[j] = dp[j] * (1 - b[i]) + dp[j - a[i]] * b[i];
25                 else dp[j] = dp[j] * (1 - b[i]);
26         double ans = 0;
27         for (int i = s - s / 2; i <= s; i++)ans += dp[i];
28         printf("%.7f\n", ans);
29     }
30     return 0;    
31 }
View Code

 

posted @ 2013-12-10 04:20  HaibaraAi  阅读(93)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3