Predictopus | codechef solution

Problem

Chef Datta likes betting in Indian Premier League very much.

He has 10000 rupees. Today the match is between team A and team B. The winning probability of team A is PA, and hence winning probability of team B is PB = 1 − PA.
Datta is free to bet any integral amount of money on any of the two teams as long as the total amount of money bet is at most 10000 rupees. Help him know the expected amount of money he will eventually have if today he places his bet(s) optimally.

Rules of the game:

If team X with winning probability PX actually wins and someone bets M rupees on this team, he will gain (2*(1−PX)) * M rupees.

If team X with winning probability PX actually loses and someone bets N rupees on this team, he will just lose N rupees.

Input

First line contains single integer T, the number of testcases. Then T lines follow, each line contains PA the probability that team A wins.

Output

For each test case output single line containing the expected amount of money Datta will eventually have today if he places his bet(s) optimally. Your answer will be accepted if the absolute error is less than 10−6.

Constraints

• 1 ≤ T ≤ 100001 (105+1)
• 0.0 ≤ PA ≤ 1.0
• PA has at most 5 digits after the decimal point.

Input
Output
```1
0.510```
`10098`

Explanation:

Look at the following situation:

If chef Datta bets 6,000 on team A and 4,000 on team B, the expected amount of money he will have after the bet is settled is 10,018. Apparently that is not the best he can do ;)

Code(C++):-

#include <bits/stdc++.h>
#include <iomanip>

using namespace std;

#define rep(i, init, final) for (long long i{init}; i < final; i++)
#define rng(ar) for (auto element : ar)
#define input(ar, n) for (long long i{}; i<n; i++) { cin >> ar[i]; }
#define print(ar) for(auto i:ar) { cout << i << " " ;}

int gcd(int a, int b){
if (a==0 || b==0)
return max(a,b);
// Recursive case //
return gcd(b,a%b);
}

void solve()
{
double p{};
cin >> p;
double b = min(p,1-p);
cout << (10000*(1+b-2*b*b)) << endl;
}

int main()
{
int t{1};
cout << fixed << setprecision(6);
cin >> t; getchar();
while (t--)
{
solve();
}
return 0;
}

Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
double a=0;
while(t-->0){
double n=sc.nextDouble();
if(n>=0.5){
a=10000 + 10000 * (1 - n) * (2 * n - 1);
}
else{
a=10000 + 10000 * n* (1 - 2 * n);
}
System.out.println(a);
}
}
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-