- 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
- 83 lines of Java from the credited upstream file ccc01s4.java.
- The implementation visibly relies on sequence storage.
- 4 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.
1234567891011121314151617181920212223 24 25import java.awt.*;26import hsa.*;27 28public class CCC2001S4Cookies29{30 public static void main (String[] args)31 {32 double[] x, y;33 int n;34 x = new double [10];35 y = new double [10];36 37 double a, b, c, s, d, ans;38 39 TextInputFile fi = new TextInputFile ("cookie5.in");40 TextOutputFile fo = new TextOutputFile ("cookie5.out");41 42 43 n = fi.readInt ();44 for (int i = 0 ; i < n ; i++)45 {46 x [i] = fi.readDouble ();47 y [i] = fi.readDouble ();48 }49 50 ans = 0;51 for (int i = 0 ; i < n ; i++)52 for (int j = i + 1 ; j < n ; j++)53 for (int k = j + 1 ; k < n ; k++)54 {55 a = Math.sqrt ((x [i] - x [j]) * (x [i] - x [j]) + (y [i] - y [j]) * (y [i] - y [j]));56 b = Math.sqrt ((x [j] - x [k]) * (x [j] - x [k]) + (y [j] - y [k]) * (y [j] - y [k]));57 c = Math.sqrt ((x [k] - x [i]) * (x [k] - x [i]) + (y [k] - y [i]) * (y [k] - y [i]));58 s = (a + b + c) / 2;59 d = 0;60 61 if ((s == 0) || (a * a + b * b - c * c < 0) || (b * b + c * c - a * a < 0) || (c * c + a * a - b * b < 0))62 {63 if (a > d)64 d = a;65 if (b > d)66 d = b;67 if (c > d)68 d = c;69 }70 else71 d = 2 * (a * b * c) / (4 * Math.sqrt (s * (s - a) * (s - b) * (s - c)));72 if (d > ans)73 ans = d;74 }75 76 fo.println (ans, 0, 2);77 78 fo.close ();79 fi.close ();80 }81}82 83