Problem solution · Java

CCC 2013 S4 - Who is Taller?

CCC 2013 S4 - Who is Taller?: 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
179 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 2013 S4 - Who is Taller?, 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

  • 179 lines of Java from the credited upstream file ccc13s4.java.
  • The implementation visibly relies on sequence storage.
  • 7 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 2013 S4 - Who is Taller? · JavaJava
Use this to learn the idea, then write your own version.
//Ivan Li, Markville secondary schoolimport java.io.*;import java.util.*;  public class Main {  public static void main(String[] args) {      FastReader sc = new FastReader();        //input int N and M      //create an array matrix map with dimensions NxN       //repeat below M times          //input int bv and ev          //bv-=1          //ev-=1          //set bv and ev as a directional neighbour          //map[bv][ev]=true;       //input int p and q      //p-=1      //q-=1      int N = sc.nextInt();      int M = sc.nextInt();       ArrayList<Integer> map[] = new ArrayList[N];       for(int i = 0; i<N; i++){        map[i] = new ArrayList<Integer>();      }       //arraylist is faster when using .get      //arraylist=LInkedlist       for(int i=0; i<M; i++){        int bv = sc.nextInt();          int ev = sc.nextInt();        bv-=1;        ev-=1;         map[bv].add(ev);      }      int p = sc.nextInt();      int q = sc.nextInt();      p-=1;      q-=1;              //setting p is starting node and q is ending node        //create a linkedlist called list      //add p to list       LinkedList<Integer> list = new LinkedList<Integer>();      list.add(p);       //create mincost array with dimension N      //fill mincost array with Integer_MAXVALUE      //set mincost[p] to 0       int mincost[] = new int[N];            Arrays.fill(mincost, Integer.MAX_VALUE);      mincost[p] = 0;               //while loop with condition List.isEmpty()      //get next node inside of list using .poll and store into variable next      //use for loop to find every neighbour of next and check if the cost to traverse is less than the current mincost of the neighbour          //if so set mincost of neighbour to cost to traverse and add neighbour to list       while (!list.isEmpty()){        int next = list.poll();                for(int i = 0; i<map[next].size(); i++){          int neighbour = map[next].get(i);          if(mincost[next]+1<mincost[neighbour]){            list.add(neighbour);            mincost[neighbour]=mincost[next]+1;          }        }      }                        //if mincost[q]!=Integer.Integer_MAXVALUE then output "yes" and return       if(mincost[q]!=Integer.MAX_VALUE){        System.out.println("yes");        return;      }              //setting q is starting node and p is ending node       list.add(q);        //fill mincost array with Integer.MAX_VALUE      //set mincost of q to 0      Arrays.fill(mincost, Integer.MAX_VALUE);      mincost[q]=0;                //while loop with condition List.isEmpty()      //get next node inside of list using .poll and store into variable next      //use for loop to find every neighbour of next and check if the cost to traverse is less than the current mincost of the neighbour          //if so set mincost of neighbour to cost to traverse and add neighbour to list        while (!list.isEmpty()){        int next = list.poll();                for(int i = 0; i<map[next].size(); i++){          int neighbour = map[next].get(i);          if(mincost[next]+1<mincost[neighbour]){            list.add(neighbour);            mincost[neighbour]=mincost[next]+1;          }        }      }          //if mincost[p]!=Integer.Integer_MAXVALUE then output "no" and return      if(mincost[p]!=Integer.MAX_VALUE){        System.out.println("no");        return;      }      //output "unknown"      System.out.println("unknown");   } public static class FastReader {        BufferedReader br;            StringTokenizer st;                public FastReader() {                br = new BufferedReader(new InputStreamReader(System.in));            }                String next() {                while (st == null || !st.hasMoreElements()) {                    try {                        st = new StringTokenizer(br.readLine());                    } catch (IOException e) {                        e.printStackTrace();                    }                }                return st.nextToken();            }                int nextInt() {                return Integer.parseInt(next());            }            long nextLong() {                return Long.parseLong(next());            }                double nextDouble() {                return Double.parseDouble(next());            }                String nextLine() {                String str = null;                try {                    str = br.readLine();                } catch (IOException e) {                    e.printStackTrace();                }                return str;            }        } } 

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 ↗