Problem solution · Java

CCC 2005 S4 - Pyramid Message Scheme

CCC 2005 S4 - Pyramid Message Scheme: a Java solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Binary search
Source
CCCSolutions
Length
69 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

Binary search

For CCC 2005 S4 - Pyramid Message Scheme, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

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

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

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 2005 S4 - Pyramid Message Scheme · JavaJava
Use this to learn the idea, then write your own version.
// The "CCC2005s4PyramidMessageScheme" class.//// determine height of a tree problem. //// assume the input of 8:a,c,a,d,a,h,b,h (the example in the problem)// my idea to to record those names in a 1D array// replacing duplicates, so as to "flatten out the tree"// so to speak and showing its true depth.//// my array starts with the root, the last name.// s (my array) = h (at start)// then as I process the names it becomes sucessively// s = h,a// s = h,a,c// s = h,a// s = h,a,d// s = h,a,// s = h// s = h,b// s = h//// the deepest it ever got was 2 (I'm counting 0,1,2 in the array). // so the savings in time is 8*10 - 2*2*10  = 40// (the new time is twice the depth of the tree * 10) import java.awt.*;import hsa.*; public class CCC2005s4PyramidMessageScheme{    static Console c;     public static void main (String[] args)    {	c = new Console ();	TextInputFile f = new TextInputFile ("s4.in"); 	int l, n, p, max;	String[] s = new String [1000];  	String[] hold = new String [1000];	String t; 	l = f.readInt ();	for (int k = 0 ; k < l ; k++)	{	    n = f.readInt ();	    p = 0;	    for (int i = 0 ; i < n ; i++)		hold [p++] = f.readLine ();	    max = 0;	    // start the array with the last name	    s [0] = hold [p - 1];	    p = 0;	    // start putting names into s	    for (int i = 0 ; i < n ; i++)	    {		int look = 0;		while (look <= p && !hold [i].equals (s [look]))		    look++;		s [look] = hold [i];		p = look;		if (p >= max)		    max = p;	    }	    c.println ("" + (n * 10 - 2 * max * 10));	}    }} 

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 ↗