Vaccine Production Solution — Codechef December Long Challenge
Problem Statement
Increasing COVID cases have created panic amongst the people of Chefland, so the government is starting to push for production of a vaccine. It has to report to the media about the exact date when vaccines will be available.
There are two companies which are producing vaccines for COVID. Company A starts producing vaccines on day D1 and it can produce V1 vaccines per day. Company B starts producing vaccines on day D2 and it can produce V2 vaccines per day. Currently, we are on day 1.
We need a total of P vaccines. How many days are required to produce enough vaccines? Formally, find the smallest integer dd such that we have enough vaccines at the end of the day d.
Input
- The first and only line of the input contains five space-separated integers D1, V1, D2, V2 and P.
Output
Print a single line containing one integer ― the smallest required number of days.
Constraints
- 1≤D1,V1,D2,V2≤100
- 1≤P≤1,000
Subtasks
Subtask #1 (30 points): D1=D2D1=D2
Subtask #2 (70 points): original constraints
Example Input 1
1 2 1 3 14
Example Output 1
3
Explanation
Since D1=D2=1D1=D2=1, we can produce V1+V2=5V1+V2=5 vaccines per day. In 33 days, we produce 1515 vaccines, which satisfies our requirement of 1414 vaccines.
Example Input 2
5 4 2 10 100
Example Output 2
9
Explanation
There are 0 vaccines produced on the first day, 10 vaccines produced on each of days 2, 3 and 4, and 14 vaccines produced on the fifth and each subsequent day. In 9 days, it makes a total of 0+10⋅3+14⋅5=100 vaccines.
Code ( Solution )
The code has been implemented in 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{// your code goes hereScanner sc = new Scanner (System.in);int d1 = sc.nextInt();int v1 = sc.nextInt();int d2 = sc.nextInt();int v2 = sc.nextInt();int p = sc.nextInt();int sum=0; int c = 0;if(d1 == d2 && d1==1){//tvd = v1 + v2;while(sum<p){ c++; sum = sum + (v1+v2); } System.out.println(c);}else{c = Math.min(d1,d2)-1;while(sum<p){if(d1>d2){ sum += v2; d2++;}else if(d2>d1){ sum += v1; d1++;}else if(d1 == d2){ sum += (v1+v2); }c++; }System.out.println(c); } }}
Hope you would have liked the article. Please give a clap to this article and follow me for more future programming related blogs.
Referenced Question Link