Problem solution · Java

CCC 2007 S2 - Boxes

CCC 2007 S2 - Boxes: 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
113 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 2007 S2 - Boxes, 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

  • 113 lines of Java from the credited upstream file ccc07s2.java.
  • The implementation visibly relies on sequence storage.
  • 5 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 2007 S2 - Boxes · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2007//// S2: Boxes//// given  a set of boxes (l,w,h)// find the smallest a given box will fit into// (smallest in terms of volume)//// This is a double sorting exercise.// The l,w,h for everything must be sorted.// Also the set of boxes needs to be sorted by volume.//// After that its just a loop to find the smallest (first)// into which a given box will fit.//// An extra box class is used to store and// sort the l, w, h, and volume for each box import java.awt.*;import hsa.*; public class CCC2007S2Boxes{    static Console c;     public static void main (String[] args)    {	c = new Console ();	TextInputFile f = new TextInputFile ("s2.1.in");	int l, w, h, n, j, m;	Box[] b;	Box hold; 	// get the boxes	n = f.readInt ();	b = new Box [n];	for (int i = 0 ; i < n ; i++)	{	    l = f.readInt ();	    w = f.readInt ();	    h = f.readInt ();	    b [i] = new Box (l, w, h);	} 	// sort the boxes by volume (insertion sort)	for (int i = 1 ; i < n ; i++)	{	    j = i - 1;	    hold = b [i];	    while (j >= 0 && b [j].volume > hold.volume)	    {		b [j + 1] = b [j];		j--;	    }	    b [j + 1] = hold;	} 	// get the boxes to test, and find the first (ie smallest)	// in set that holds it.	m = f.readInt ();	for (int i = 0 ; i < m ; i++)	{	    l = f.readInt ();	    w = f.readInt ();	    h = f.readInt ();	    hold = new Box (l, w, h);	    j = 0;	    while (j < n && (hold.l > b [j].l ||			hold.w > b [j].w ||			hold.h > b [j].h))		j++;	    if (j < n)		c.println (b [j].volume);	    else		c.println ("Item does not fit.");	}    }} class Box{    public int l, w, h;    public long volume;     public Box (int a, int b, int c)    {	int t;	l = a;	w = b;	h = c;	if (l > w)	{	    t = l;	    l = w;	    w = t;	}	if (w > h)	{	    t = w;	    w = h;	    h = t;	}	if (l > w)	{	    t = l;	    l = w;	    w = t;	}	volume = l * w * h;    }}  

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 ↗