Codechef August Challenge 2013 Save The Princess
Save The PrincessProblem code: SHIRO |
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 }
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 }
浙公网安备 33010602011771号