- 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
- 191 lines of Java from the credited upstream file ccc09s5.java.
- The implementation visibly relies on sequence storage.
- 17 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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 56import java.awt.*;57import hsa.*;58 59public class CCC2009S5Wireless60{61 62 public static int[] [] coffeeShop;63 public static int rows, cols, k;64 65 public static void main (String[] args)66 {67 int col, row, radius, bitrate, max, total;68 69 TextInputFile f = new TextInputFile ("s5.1.in");70 71 cols = f.readInt (); 72 rows = f.readInt ();73 coffeeShop = new int [rows + 1] [cols + 1];74 75 for (int i = 1 ; i <= rows ; i++)76 for (int j = 1 ; j <= cols ; j++)77 coffeeShop [i] [j] = 0;78 79 int k = f.readInt ();80 total = 0;81 for (int i = 1 ; i <= k ; i++)82 {83 row = f.readInt ();84 col = f.readInt ();85 radius = f.readInt ();86 bitrate = f.readInt ();87 if (col + radius > 30033 && radius > col)88 total += bitrate;89 else90 process (col, row, radius, bitrate);91 }92 93 max = maxBitRate ();94 System.out.println (max + total);95 System.out.println (countMaxBitRate (max));96 }97 98 99 public static void process (int c, int r, int radius, int bitrate)100 {101 102 int start = Math.max (1, c - radius);103 int stop = Math.min (cols, c + radius);104 int uplimit = Math.min (rows, r + radius);105 int downlimit = Math.max (1, r - radius);106 int square = radius * radius;107 int i, j;108 int t1, t2, t3; 109 for (i = r ; i <= uplimit ; i++)110 {111 t1 = i - r;112 t2 = t1 * t1;113 t3 = square - t2;114 while (((start - c) * (start - c)) > t3)115 start++;116 while (((stop - c) * (stop - c)) > t3)117 stop--;118 119 120 121 for (j = start ; j < stop - 8 ; j++)122 {123 coffeeShop [i] [j++] += bitrate;124 coffeeShop [i] [j++] += bitrate;125 coffeeShop [i] [j++] += bitrate;126 coffeeShop [i] [j++] += bitrate;127 coffeeShop [i] [j++] += bitrate;128 coffeeShop [i] [j++] += bitrate;129 coffeeShop [i] [j++] += bitrate;130 coffeeShop [i] [j++] += bitrate;131 coffeeShop [i] [j++] += bitrate;132 coffeeShop [i] [j] += bitrate;133 }134 while (j <= stop)135 coffeeShop [i] [j++] += bitrate;136 137 }138 start = Math.max (1, c - radius);139 stop = Math.min (cols, c + radius);140 for (i = r - 1 ; i >= downlimit ; i--)141 {142 t1 = i - r;143 t2 = t1 * t1;144 t3 = square - t2;145 while (((start - c) * (start - c)) > t3)146 start++;147 while (((stop - c) * (stop - c)) > t3)148 stop--;149 for (j = start ; j < stop - 8 ; j++)150 {151 coffeeShop [i] [j++] += bitrate;152 coffeeShop [i] [j++] += bitrate;153 coffeeShop [i] [j++] += bitrate;154 coffeeShop [i] [j++] += bitrate;155 coffeeShop [i] [j++] += bitrate;156 coffeeShop [i] [j++] += bitrate;157 coffeeShop [i] [j++] += bitrate;158 coffeeShop [i] [j++] += bitrate;159 coffeeShop [i] [j++] += bitrate;160 coffeeShop [i] [j] += bitrate;161 }162 while (j <= stop)163 coffeeShop [i] [j++] += bitrate;164 }165 }166 167 168 public static int maxBitRate ()169 {170 int max = 0;171 for (int i = 1 ; i <= rows ; i++)172 for (int j = 1 ; j <= cols ; j++)173 if (coffeeShop [i] [j] > max)174 max = coffeeShop [i] [j];175 return max;176 }177 178 179 public static int countMaxBitRate (int max)180 {181 int count = 0;182 for (int i = 1 ; i <= rows ; i++)183 for (int j = 1 ; j <= cols ; j++)184 if (coffeeShop [i] [j] == max)185 count++;186 return count;187 }188}189 190 191