Cookie Clicker Alpha – Google Code Jam 2014
This was the second problem for Google Code Jam Qualification round 2014, if you are able to solve this problem with the first one(which is very easy) you will be eligible for the next round.
Problem
In this problem, you start with 0 cookies. You gain cookies at a rate of 2 cookies per second, by clicking on a giant cookie. Any time you have at least C cookies, you can buy a cookie farm. Every time you buy a cookie farm, it costs you C cookies and gives you an extra F cookies per second.
Once you have X cookies that you haven’t spent on farms, you win! Figure out how long it will take you to win if you use the best possible strategy.
Example
Suppose C=500.0, F=4.0 and X=2000.0. Here’s how the best possible strategy plays out:
- You start with 0 cookies, but producing 2 cookies per second.
- After 250 seconds, you will have C=500 cookies and can buy a farm that producesF=4 cookies per second.
- After buying the farm, you have 0 cookies, and your total cookie production is 6 cookies per second.
- The next farm will cost 500 cookies, which you can buy after about 83.3333333seconds.
- After buying your second farm, you have 0 cookies, and your total cookie production is 10 cookies per second.
- Another farm will cost 500 cookies, which you can buy after 50 seconds.
- After buying your third farm, you have 0 cookies, and your total cookie production is 14 cookies per second.
- Another farm would cost 500 cookies, but it actually makes sense not to buy it: instead you can just wait until you have X=2000 cookies, which takes about142.8571429 seconds.
Total time: 250 + 83.3333333 + 50 + 142.8571429 = 526.1904762 seconds.
Notice that you get cookies continuously: so 0.1 seconds after the game starts you’ll have 0.2 cookies, and π seconds after the game starts you’ll have 2π cookies.
Input
The first line of the input gives the number of test cases, T. T lines follow. Each line contains three space-separated real-valued numbers: C, F and X, whose meanings are described earlier in the problem statement.
C, F and X will each consist of at least 1 digit followed by 1 decimal point followed by from 1 to 5 digits. There will be no leading zeroes.
Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the minimum number of seconds it takes before you can have X delicious cookies.
We recommend outputting y to 7 decimal places, but it is not required. y will be considered correct if it is close enough to the correct number: within an absolute or relative error of 10-6. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ C ≤ 500.
1 ≤ F ≤ 4.
1 ≤ X ≤ 2000.
Large dataset
1 ≤ C ≤ 10000.
1 ≤ F ≤ 100.
1 ≤ X ≤ 100000.
Sample
Input | Output |
4 30.0 1.0 2.0 30.0 2.0 100.0 30.50000 3.14159 1999.19990 500.0 4.0 2000.0
|
Case #1: 1.0000000 Case #2: 39.1666667 Case #3: 63.9680013 Case #4: 526.1904762
|
So I first solved it through a recursive function. (I still don’t know why I did this stupid thing). So small-input file gave correct results but the program terminated during large-input evaluation (stack overflow error). :O
Here is the code (recursive):
import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class cookie { static int i = 0; static Vector<Double> vec; static double target=0; static double fRate = 0; static double fCost = 0; public static double fun(double prevFarmBuyTimes, double rate){ if(i==0){ vec.add(target/rate); } double farmTime = prevFarmBuyTimes+fCost/rate; rate+=fRate; double WaitingTime = target/rate; double projection = farmTime+WaitingTime; vec.add(projection); i++; if(vec.elementAt(i-1)>vec.elementAt(i)){ return fun(farmTime, rate); } else { return vec.elementAt(i-1); } } public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new FileReader("d:\\B-large-practice.in")); BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\result.txt")); int ncases = Integer.parseInt(br.readLine()); int index = 1; while(index<=ncases){ Pattern p = Pattern.compile("\\d+.\\d+"); Matcher m = p.matcher(br.readLine()); if (m.find()) fCost = Double.parseDouble(m.group()); if (m.find()) fRate = Double.parseDouble(m.group()); if (m.find()) target = Double.parseDouble(m.group()); //LOGIC STARTS HERE vec = new Vector<Double>(); i=0; double time = fun(0,2); bw.write("Case #"+index+": "+time); bw.newLine(); bw.flush(); System.out.println(time); index++; } } }
And here is the improved version without recursion.(Gives correct results for large-inputs):
import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class cookie { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new FileReader("d:\\B-large-practice.in")); BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\result.txt")); int ncases = Integer.parseInt(br.readLine()); int index = 1; int i = 0; Vector<Double> vec; double target=0; double fRate = 0; double fCost = 0; while(index<=ncases){ Pattern p = Pattern.compile("\\d+.\\d+"); Matcher m = p.matcher(br.readLine()); if (m.find()) fCost = Double.parseDouble(m.group()); if (m.find()) fRate = Double.parseDouble(m.group()); if (m.find()) target = Double.parseDouble(m.group()); vec = new Vector<Double>(); i=0; double rate =2; vec.add(target/rate); double result = 0; double prevFarmBuyTimes=0; while(true){ double farmTime = prevFarmBuyTimes+fCost/rate; prevFarmBuyTimes=farmTime; rate+=fRate; double WaitingTime = target/rate; double projection = farmTime+WaitingTime; i++; vec.add(projection); if(vec.elementAt(i-1)<vec.elementAt(i)){ result = vec.elementAt(i-1); break; } } System.out.println(result); bw.write("Case #"+index+": "+result); bw.newLine(); bw.flush(); index++; } } }
Your Answer
More puzzles to try-
Hand can\’t Clap
What has hands, but can’t clap?Read More »Bacteria colony on Petri dish puzzle
A Petri dish hosts a healthy colony of bacteria. Once a minute every bacterium divides into two. The colony was ...Read More »Answer in question
When is the answer in the question?Read More »Mall Store Puzzle
John Went to the nearby store in a Mall to buy something for her home. Below is the conversation between ...Read More »Coin game with devil riddle
you die and the devil says he’ll let you go to heaven if you beat him in a game. the ...Read More »Mislabeled Jar Puzzle
This is the most commonly asked interview puzzle. You have 3 jars that are all mislabeled. One jar contains Apple, ...Read More »Number magic – Mathematical puzzle
If you multiply me by 2, subtract 1, and read the reverse the result you’ll find me. Which numbers can ...Read More »Black and White Hats Puzzle
Naga a safari in the kajiranga jungle capture three men. The naga give the men a single chance to escape uneaten. ...Read More »Cut not to eat
What is cut on a table, but is never eaten?Read More »Finding name of the ancient king puzzle
500 at the beginning, 500 at the end, 5 in the middle is seen, The first of all letters, the ...Read More »Find the Missing Number
Triangles Count
Count the number of triangles in the below picture?Read More »Matchstick Game
Can you find out a way through which you can make five squares out of the given figure by moving ...Read More »Distance between Villages
There is a straight highway. Four different villages lie on that highway. The distance between them is different. The third ...Read More »Can be saved but not made
What can be saved but not made, can be taken but not given, can be short or long, and can ...Read More »A man with a car riddle
A man with a car on his head is called…….Read More »Color of Tie
Mister Black , Brown and Green are eating at a restaurant. Each is wearing a coloured tie. The ties are ...Read More »Making The Century
Use the digits from 1 up to 9 and make 100. Follow the rules. => Each digit should be used ...Read More »Boxing Match Riddle
Usually, a boxing match consists of twelve rounds. In a particular friendly match between two heavyweights, the match was finished ...Read More »Once he could see
It does not have eyes, but once it did see. Once it had thoughts, but now it is white and ...Read More »