Problem solution · Turing

CCC 1997 P3 - Double Knockout Competition

CCC 1997 P3 - Double Knockout Competition: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
62 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

Direct simulation

For CCC 1997 P3 - Double Knockout Competition, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

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

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 1997 P3 - Double Knockout Competition · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1997% problem C: Double Knockout Competition%% you've got: undefeated, 1-loss and eliminated teams% at each step: %     1/2 the 1-loss become eliminated% and 1/2 the undefeated become 1-loss % in other words: eliminated = eliminated + (1/2 1-loss teams)%                 1 - loss   = 1-loss  - (1/2 1-loss teams) + (1/2 of the undefeated)%                 undefeated = undefeated - (1/2 undefeated) % except at end with 1 undefeated and 1 1-loss%                          they play and the undefeated loses!% then they play and you're done: there is 1 l-loss team. % file handling is used, the number of starting teams is given% then the number of teams themselves  var infile : string := "dkc.in"var outfile : string := "dkc.out"var fi, fo : intvar n, x, ud, ol, el, count : int open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n    get : fi, x    ud := x    ol := 0    el := 0    count := 0    loop        put : fo, "Round: ", count, ": ", ud, " undefeated, ", ol,            " one-loss, ", el, " eliminated"        exit when ud = 0 and ol = 1        if ud = 1 and ol = 1 then            el := el            ol := 2            ud := 0        else            el := el + ol div 2            ol := ol - (ol div 2) + (ud div 2)            ud := ud - (ud div 2)        end if        count := count + 1    end loop    put : fo, "There are ", count, " rounds."    put : fo, ""end for close : ficlose : fo      

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 ↗