- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 105 lines of Java from the credited upstream file ccc04s4.java.
- The implementation visibly relies on sequence storage.
- 1 loop block detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123456789101112131415161718192021222324252627282930313233 34import hsa.*;35 36public class S4SpaceTurtlePeng37{38 static Console c;39 static double tx, ty, tz;40 static double sx, sy, sz;41 static double x, y, z, newX, t;42 43 public static void main (String[] args)44 {45 c = new Console ();46 double closest, distance, d;47 char turn;48 String file;49 50 c.print ("file name: ");51 file = c.readString ();52 TextInputFile fi = new TextInputFile (file);53 54 tx = fi.readDouble (); 55 ty = fi.readDouble ();56 tz = fi.readDouble ();57 58 sx = fi.readDouble (); 59 sy = fi.readDouble ();60 sz = fi.readDouble ();61 62 x = sx - tx;63 y = sy - ty;64 z = sz - tz;65 66 closest = x * x + y * y + z * z;67 do68 {69 distance = fi.readDouble ();70 turn = fi.readChar ();71 72 newX = x - distance;73 74 if (newX * x < 0)75 closest = Math.min (closest, y * y + z * z);76 else77 closest = Math.min (closest, newX * newX + y * y + z * z);78 x = newX;79 t = x;80 if (turn == 'L')81 {82 x = y;83 y = -t;84 }85 else if (turn == 'R')86 {87 x = -y;88 y = t;89 }90 else if (turn == 'U')91 {92 x = z;93 z = -t;94 }95 else96 {97 x = -z;98 z = t;99 }100 }101 while (turn != 'E');102 c.println ((int) ((Math.sqrt (closest) * 100) + 0.5) / 100.0);103 }104}105