Problem solution · Python

CCC 2012 S3 - Absolutely Acidic

CCC 2012 S3 - Absolutely Acidic: a Python 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
68 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 2012 S3 - Absolutely Acidic, 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

  • 68 lines of Python from the credited upstream file ccc12s3.py.
  • The implementation visibly relies on sequence storage.
  • 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 2012 S3 - Absolutely Acidic · PythonPython
Use this to learn the idea, then write your own version.
# S3 2012: Absolutely Acidic## For the largest files this takes about 45sec.# All that is spent reading. The sort takes# less than 1 second. ## The algorithm is very straight forward;#  read the file and count the readings and store in a list of 1..1000#  sort the list descending (sub sorting helps case 1)#  max and second max are at the first so determine the case#  and calculate the answer.  class Info:    def __init__(self, r, c):        self.reading = r        self.count = c         file = open("s3.5.in", 'r') # initialize the listfreq = []for i in range(1001):    freq.append(Info(i,0)) # read the file and add the frequencies# need to skip the first line.firstline = Truefor line in file:    if firstline:        firstline = False    else:        r = eval(line)        freq[r].count = freq[r].count + 1 # sort freq, by count, subsorted by the reading# use insertion sort methodfor i in range (1,1001):    hold = freq[i]    j = i - 1    while j >= 0 and (freq[j].count < hold.count or                      (freq[j].count == hold.count and                       freq[j].reading < hold.reading)):        freq[j+1] = freq[j]        j = j - 1    freq[j+1] = hold     # determine the typemultihigh = freq[0].count == freq[2].countmultisecondhigh = freq[0].count != freq[1].count and freq[1].count == freq[2].count        # get the answerif multihigh:    i = 1    while i < 1001 and freq[0].count == freq[i].count:        i = i + 1     print abs(freq[0].reading - freq[i-1].reading)elif multisecondhigh:    i = 3    m = abs(freq[0].reading - freq[1].reading)    while i < 1001 and freq[1].count == freq[i].count:        m = max (m, abs(freq[0].reading - freq[i].reading))        i = i + 1     print melse:    print abs(freq[0].reading - freq[1].reading)                       

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 ↗