- 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
- 68 lines of Java from the credited upstream file ccc10s1.java.
- The implementation visibly relies on sequence storage.
- 1 loop block 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.
123456 7import java.awt.*;8import hsa.*;9 10public class S1201011{12 13 14 public static void main (String[] args)15 {16 TextInputFile c;17 c = new TextInputFile ("s1.4.in");18 19 int n, r, s, d, total;20 String name;21 int best1, best2;22 String bestName1, bestName2;23 24 n = c.readInt ();25 if (n > 0)26 {27 name = c.readString ();28 r = c.readInt ();29 s = c.readInt ();30 d = c.readInt ();31 total = 2 * r + 3 * s + d;32 best1 = total;33 bestName1 = name;34 bestName2 = "";35 best2 = 0;36 for (int i = 1 ; i < n ; i++)37 {38 name = c.readString ();39 r = c.readInt ();40 s = c.readInt ();41 d = c.readInt ();42 total = 2 * r + 3 * s + d;43 if ((total > best1) || (total == best1 && name.compareTo (bestName1) < 0))44 {45 best2 = best1;46 bestName2 = bestName1;47 best1 = total;48 bestName1 = name;49 }50 else if ((total > best2) || (total == best2 && name.compareTo (bestName2) < 0))51 {52 best2 = total;53 bestName2 = name;54 55 }56 }57 58 if (n == 1)59 System.out.println (bestName1);60 else61 {62 System.out.println (bestName1);63 System.out.println (bestName2);64 }65 }66 }67}68