Whenever a company trades securities, there are various risks involved with the trade. Risk analysis is done for each trade in order to make the maximum profit from that trade.
Each available trade can have the following properties :
Probability that the trade will make a profit (p).
Probability that the trade will make a loss (1 - p).
Potential profit of the trade (x).
Potential loss of the trade (y).
Find and print the maximum expected amount of money the company can make by performing at most m of the n trades, given the values of m, n, x, y and p.
INPUT FORMAT :::
The first line contains two space - separated integers denoting the respective values
n (the number of trades available) and m (the maximum number of trades allowed).
The second line contains space - separated floating - point numbers
describing the respective values of pi,
where each pi denotes the probability that the ith transaction will result in a profit.
The third line contains space - separated floating - point numbers
describing the respective values of xi,
where each xi denotes the potential profit of the ith transaction.
The fourth line contains space - separated floating - point numbers
describing the respective values of yi,
where each yi denotes the potential loss of the ith transaction.
CONSTRAINTS :::
1 ≤ n, m ≤ 100000
0 ≤ x, y ≤ 100
0 ≤ p ≤ 1
All x, y and p are floating - point numbers
scaled to exactly two decimal places (i.e 2.45 format).
OUTPUT FORMAT :::
Print the maximum expected amount of money that can be made by performing at most m of the n available trades. Scale your answer to exactly 2 decimal places.
EXAMPLE 1::
Input ::
4 2
0.50 0.50 0.50 0.50
4.00 1.00 2.00 3.00
4.00 0.50 1.00 1.00
Output ::
1.50
i.e. There are n = 4 transactions available and we can perform at most m = 2 of them.
If third and fourth transactions are performed, the expected amount of money made is:
(0.5 * 2.0) - ((1 - 0.5) * 1.0) + (0.5 * 3.0) - ((1 - 0.5) * 1.0) = 1.5.
Since this is greater than all of the other possibilities, 1.50 is our answer.
EXAMPLE 2 ::
Input ::
2 2
0.90 0.50
1.00 0.50
100.00 0.40
Output ::
0.05
i.e. There are n = 2 transactions available and we can perform at most m = 2 of them.
If the second transaction is performed, the expected amount of money made is:
(0.5 * 0.5) - ((1 - 0.5) * 0.4) = 0.05.
Since this is greater than all of the other possibilities we could calculate,
we print 0.05 as our answer.
HERE IS MY APPROACH AND C++ PROGRAM ::
```
#include<bits/std++.h>
using namespace std;
void maxMoney(int n, int maxtrades, vector<double> p, vector<double> x, vector<double> y)
{
double profit = 0;
double t;
vector<double> v;
for (int i = 0; i < n; i++)
{
t = p[i] * (x[i] + y[i]) - y[i];
if (t > 0)
v.push_back(t);
}
sort(v.begin(), v.end(), greater<double>());
int lenn = v.size();
int siz = min(maxtrades, lenn);
for (int i = 0; i <= siz; i++)
profit += v[i];
int main()
{
int n, m;
cin >> n >> m;
vector<double> p(n), x(n), y(n);
for (int i = 0; i < n; i++)
cin >> p[i];
for (int i = 0; i < n; i++)
cin >> x[i];
for (int i = 0; i < n; i++)
cin >> y[i];
maxMoney(n, m, p, x, y);
return 0;
}
```
**OR**
```
#include<bits/stdc++.h>
using namespace std;
void maximumExpectedMoney(int n, int m, vector<long long> p, vector<long long> x, vector<long long> y)
{
vector<long long> v;
for (int i = 0; i < n; i++)
{
long long t = p[i] * x[i] - (100 - p[i]) * y[i];
if (t > 0)
v.push_back(t);
}
sort(v.begin(), v.end(), greater<long long>());
int lenn = v.size();
int siz = min(m, lenn);
long long profit = 0;
for (int i = 0; i <= siz; i++)
profit += v[i];
int mod = profit % 100;
profit /= 100;
if (mod > 49) profit++;
int a = profit % 10;
profit /= 10;
int b = profit % 10;
profit /= 10;
cout << profit << "." << b << a << "\n";
}
int main()
{
int n, m;
double f;
cin >> n >> m;
vector<long long> p(n), x(n), y(n);
for (int i = 0; i < n; i++)
{
cin >> f;
p[i] = llroundf(f * 100.0);
}
for (int i = 0; i < n; i++)
{
cin >> f;
x[i] = llroundf(f * 100.0);
}
for (int i = 0; i < n; i++)
{
cin >> f;
y[i] = llroundf(f * 100.0);
}
maximumExpectedMoney(n, m, p, x, y);
return 0;
}
```
I am getting maximum 83 or 84 out of 90 test cases correct in both cases, NOT MORE, no matter what I do.
Many people answered the problem with CORRECT SOLUTION (90 / 90 testcases correct).
If I round-off profit values after multiptying p[i]*x[i], i.e. side by side during the calculation and not at last,
then almost all test cases gives WRONG ANSWER.
Since my second approach is exact and provides 0% error, but then aslo 83 cases correct.
So what is the issue or what precision or output is exactly required by the problem ??
Can anyone please help and provide the correct Code ??