- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- 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.
Use this to learn the idea, then write your own version.
123456789101112131415161718 19import java.awt.*;20import hsa.*;21 22public class CCC2007S2Boxes23{24 static Console c;25 26 public static void main (String[] args)27 {28 c = new Console ();29 TextInputFile f = new TextInputFile ("s2.1.in");30 int l, w, h, n, j, m;31 Box[] b;32 Box hold;33 34 35 n = f.readInt ();36 b = new Box [n];37 for (int i = 0 ; i < n ; i++)38 {39 l = f.readInt ();40 w = f.readInt ();41 h = f.readInt ();42 b [i] = new Box (l, w, h);43 }44 45 46 for (int i = 1 ; i < n ; i++)47 {48 j = i - 1;49 hold = b [i];50 while (j >= 0 && b [j].volume > hold.volume)51 {52 b [j + 1] = b [j];53 j--;54 }55 b [j + 1] = hold;56 }57 58 59 60 m = f.readInt ();61 for (int i = 0 ; i < m ; i++)62 {63 l = f.readInt ();64 w = f.readInt ();65 h = f.readInt ();66 hold = new Box (l, w, h);67 j = 0;68 while (j < n && (hold.l > b [j].l ||69 hold.w > b [j].w ||70 hold.h > b [j].h))71 j++;72 if (j < n)73 c.println (b [j].volume);74 else75 c.println ("Item does not fit.");76 }77 }78}79 80class Box81{82 public int l, w, h;83 public long volume;84 85 public Box (int a, int b, int c)86 {87 int t;88 l = a;89 w = b;90 h = c;91 if (l > w)92 {93 t = l;94 l = w;95 w = t;96 }97 if (w > h)98 {99 t = w;100 w = h;101 h = t;102 }103 if (l > w)104 {105 t = l;106 l = w;107 w = t;108 }109 volume = l * w * h;110 }111}112 113