Problem solution · Java

CCC 2005 S5 - Pinball Ranking

CCC 2005 S5 - Pinball Ranking: 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
100 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 2005 S5 - Pinball Ranking, 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

  • 100 lines of Java from the credited upstream file ccc05s5.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 2005 S5 - Pinball Ranking · JavaJava
Use this to learn the idea, then write your own version.
// The "CCC2005s5PinballRankingMerge" class.//// this solution is due to Sean Henderson of University of Toronto//// the insight is that rank for score x is essentially// the number of inversions from 0 to x (plus 1). An inversion// is when a[x] > a[y] when x < y.// so if the total inversions for the entire array is I// the average rankings is (I + N) / N//// The number of inversions of the array can be determined// by using merge sort. The idea is if you are merging// a[x,y) with a[y,z) then the number of inversions is the total// of the number of elements in a[x,y) each time one is taken// from a[y,z).//// For example suppose you were sorting://  5 8 2 5 7 1//// after 1 pass it would be//  5 8 2 5 1 7 with a total = 1 because there was 1 case//              where the 7 and 1 were out of order//// on the second pass 5 8 and 2 5 are merged together// in this case the total = 2 (5 8 versus 2) plus 1// (for the 8 versus 5)// total inversions so far is 4, and the array looks like//  2 5 5 8 1 7//// on the last pass the total = 4 (2558 vs 1) plus 1// (for the 8 vs 7)// giving a grand total inversions of 9.//// the average rankings is then (9+6)/6 = 2.5//// this is correct because the rankings found normally are:// (1 + 1 + 3 + 2 + 2 + 6) / 6 = 2.5 import java.awt.*;import hsa.*; public class CCC2005s5PinballRankingMerge{    static Console c;    static double I;     public static void main (String[] args)    {	c = new Console ();	TextInputFile f = new TextInputFile ("s5.9.in");	I = 0;	int[] a = new int [100001]; 	int n;	n = f.readInt ();	for (int x = 0 ; x < n ; x++)	    a [x] = f.readInt ();	mergeSort (a, 0, n - 1);	c.println ((I + n) / n, 0, 2);    }      // this is an inclusive a[x,z] merge sort    public static void mergeSort (int[] a, int x, int z)    {	if (x < z)	{	    int y = (x + z) / 2;	    mergeSort (a, x, y);	    mergeSort (a, y + 1, z);	    I += merge (a, x, y, z);	}    }      public static double merge (int[] a, int x, int y, int z)    {	int[] newa = new int [z - x + 1];	int xx = x;	int yy = y + 1;	int k = 0;	double total = 0;	while (xx <= y && yy <= z)	    if (a [xx] <= a [yy])		newa [k++] = a [xx++];	    else	    {		newa [k++] = a [yy++];		total = total + (y + 1 - xx);	    }	while (xx <= y)	    newa [k++] = a [xx++];	while (yy <= z)	    newa [k++] = a [yy++];	for (xx = x ; xx <= z ; xx++)	    a [xx] = newa [xx - x];	return total;    }} 

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 ↗