Problem solution · Turing

CCC 2000 S2 - Babbling Brooks

CCC 2000 S2 - Babbling Brooks: a Turing solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Sliding window or two pointers
Source
CCCSolutions
Length
67 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For CCC 2000 S2 - Babbling Brooks, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 67 lines of Turing from the credited upstream file ccc00s2.t.
  • The implementation keeps its working state in language-native values and containers.
  • No explicit loop blocks 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.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2000 S2 - Babbling Brooks · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 2000% problem J4: Babbling Brooks % calculate the flow in 1 to n rivers % file handling is used% n is given, then the flow in each of the n rivers% 99 - split, the river number and % of flow to left% 88 - join, the river number that is rejoined with one to its right% 77 - stop processing % print the flow (rounded) in rivers 1 to n% relatively simple array processing is used. Trick is to insert and delete% elements with shifting. % use reals and round answers.  var fi, fo : intvar f : stringvar b : array 1 .. 100 of realvar x : intvar j : intvar p, hold : realvar n : int put "Input file: " ..get fopen : fi, f, getput "Output file: " ..get fopen : fo, f, putget : fi, nfor i : 1 .. n    get : fi, b (i)end forloop    get : fi, x    exit when x = 77    if x = 99 then        get : fi, j        get : fi, p        n := n + 1        for decreasing k : n .. j + 2            b (k) := b (k - 1)        end for        hold := b (j)        b (j) := hold * (p / 100)        b (j + 1) := hold - b (j)    elsif x = 88 then        get : fi, j        b (j) := b (j) + b (j + 1)        for k : j + 2 .. n            b (k - 1) := b (k)        end for        n := n - 1    end ifend loopfor k : 1 .. n    put : fo, round (b (k)), " " ..end forput : fo, ""close : ficlose : foput "Done!"  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗