Problem solution · Java

CCC 2001 S4 - Cookies

CCC 2001 S4 - Cookies: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
83 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2001 S4 - Cookies, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 83 lines of Java from the credited upstream file ccc01s4.java.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2001 S4 - Cookies · JavaJava
Use this to learn the idea, then write your own version.
// The "CCC2001S4Cookie" class.//// this problem has been a great source of frustatation. It has taken// 4 years to get a "proper" solution, thanks to Richard Peng. :-)//// The algorithm is to use the formula for finding the radius of a// circle circumscribing a triangle with sides a, b, c.// that formula is: r = abc/4*sqrt(s(s-a)(s-b)(s-c) where// s is the semi perimeter: (a+b+c)/2//// The minimum diameter needed to encompass three points is thus// this circle (if the points make an acute triangle) or the// length of the longest side in an obtuse triangle.//// So from a programming point of view:// the idea is to take every possible combination of 3 points,// calculate the diameter of the circle for each// and the maximum diameter of these is the answer.//// File I/O// Input: n and the locations of n chips// Ouput: diameter of circle which enclosed them.  import java.awt.*;import hsa.*; public class CCC2001S4Cookies{    public static void main (String[] args)    {	double[] x, y;	int n;	x = new double [10];	y = new double [10]; 	double a, b, c, s, d, ans; 	TextInputFile fi = new TextInputFile ("cookie5.in");	TextOutputFile fo = new TextOutputFile ("cookie5.out"); 	// get the chip locations	n = fi.readInt ();	for (int i = 0 ; i < n ; i++)	{	    x [i] = fi.readDouble ();	    y [i] = fi.readDouble ();	} 	ans = 0;	for (int i = 0 ; i < n ; i++)	    for (int j = i + 1 ; j < n ; j++)		for (int k = j + 1 ; k < n ; k++)		{		    a = Math.sqrt ((x [i] - x [j]) * (x [i] - x [j]) + (y [i] - y [j]) * (y [i] - y [j]));		    b = Math.sqrt ((x [j] - x [k]) * (x [j] - x [k]) + (y [j] - y [k]) * (y [j] - y [k]));		    c = Math.sqrt ((x [k] - x [i]) * (x [k] - x [i]) + (y [k] - y [i]) * (y [k] - y [i]));		    s = (a + b + c) / 2;		    d = 0;		    // check for division by zero or obtuse triangle		    if ((s == 0) || (a * a + b * b - c * c < 0) || (b * b + c * c - a * a < 0) || (c * c + a * a - b * b < 0))		    {			if (a > d)			    d = a;			if (b > d)			    d = b;			if (c > d)			    d = c;		    }		    else			d = 2 * (a * b * c) / (4 * Math.sqrt (s * (s - a) * (s - b) * (s - c)));		    if (d > ans)			ans = d;		} 	fo.println (ans, 0, 2); 	fo.close ();	fi.close ();    }}  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗