HACKER RANK Algorithms - Implementation - Taum and B'day - Solution.'C++'

                            Algorithms - Implementation - Taum and B'day - Solution
Problem Statement
Taum is planning to celebrate the birthday of his friend, Diksha. There are two types of gifts that Diksha wants from Taum: one is black and the other is white. To make her happy, Taum has to buy B number of black gifts and W number of white gifts.
  • The cost of each black gift is X units.
  • The cost of every white gift is Y units.
  • The cost of converting each black gift into white gift or vice versa is Z units.
Help Taum by deducing the minimum amount he needs to spend on Diksha's gifts.
Input Format
The first line will contain an integer T which will be the number of test cases.
There will be T pairs of lines. The first line of each test case will contain the values of integersB and W. Another line of each test case will contain the values of integers XY, and Z.
Constraints 
1T10 
0X,Y,Z,B,W109
Output Format
T lines, each containing an integer: the minimum amount of units Taum needs to spend on gifts.
Sample Input
5
10 10
1 1 1
5 9
2 3 4
3 6
9 1 1
7 7
4 2 1
3 3
1 9 2
Sample Output
20
37
12
35
12
Explanation
  • Sample Case #01: 
    There is no benefit to converting the white gifts into black or the black gifts into white, so Taum will have to buy each gift for 1 unit. So cost of buying all gifts will be: 101+101=20.
  • Sample Case #02: 
    Again, we can't decrease the cost of black or white gifts by converting colors. We will buy gifts at their original price. So cost of buying all gifts will be: 52+93=10+27=37.
  • Sample Case #03: 
    We will buy white gifts at their original price, 1. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 1+1=2. So cost of buying all gifts will be: 32+61=12.
  • Sample Case #04: 
    Similarly, we will buy white gifts at their original price, 2. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 2+1=3. So cost of buying all gifts will be: 73+72=35.
  • Sample Case #05: We will buy black gifts at their original price, 1. For white gifts, we will first black gifts worth 1 unit and color them to white with another 2 units, so cost for white gifts is reduced to 3 units. So cost of buying all gifts will be: 31+33=3+9=12.

    Solution:

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
     int t;
     cin >> t;
     while (t--)
     {
      long long b, w, x, y, z;
      cin >> b >> w >> x >> y >> z;
      long long ans1 = (b + w) * x + w * z;
      long long ans2 = (b + w) * y + b * z;
      long long ans3 = b * x + w * y;
      long long ans = min(min(ans1, ans2), ans3);
      cout << ans << endl;
     }
     return 0;
    }

    Thanks for Visiting, Hope this helps you....


Comments

Popular Posts