- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 100 lines of Java from the credited upstream file ccc09s2.java.
- The implementation visibly relies on sequence storage.
- 7 loop blocks detected.
Complexity
Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12345678910111213141516171819202122import java.awt.*;23import hsa.*;24 25public class CCC2009S2Lights26{27 28 public static void main (String[] args)29 {30 Console c;31 String[] rows;32 String[] above = new String [256];33 int aboveSize;34 String[] below = new String [256];35 int belowSize;36 int r, l, k;37 String newrow;38 39 c = new Console ();40 41 42 TextInputFile f = new TextInputFile ("s2.5.in");43 r = f.readInt ();44 l = f.readInt ();45 rows = new String [r];46 for (int i = 0 ; i < r ; i++)47 {48 rows [i] = "";49 for (int j = 0 ; j < l ; j++)50 rows [i] = rows [i] + f.readString ();51 }52 53 54 above [0] = rows [0];55 aboveSize = 1;56 belowSize = 1;57 for (int i = 1 ; i < r ; i++)58 {59 below [0] = rows [i];60 belowSize = 1;61 for (int j = 0 ; j < aboveSize ; j++)62 {63 newrow = pushButton (above [j], below [0]);64 65 66 k = 0;67 while (k < belowSize && !below [k].equals (newrow))68 k++;69 if (k >= belowSize)70 {71 below [belowSize] = newrow;72 belowSize++;73 }74 }75 76 77 for (int j = 0; j < belowSize; j++)78 above[j] = below[j];79 aboveSize = belowSize;80 }81 c.println (belowSize);82 }83 84 85 86 87 public static String pushButton (String s, String t)88 {89 String x;90 x = "";91 for (int i = 0 ; i < s.length () ; i++)92 if (s.charAt (i) == t.charAt (i))93 x = x + "0";94 else95 x = x + "1";96 return x;97 }98}99 100