Problem solution · Python

CCC 2015 S2 - Jerseys

CCC 2015 S2 - Jerseys: 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
36 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 2015 S2 - Jerseys, 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

  • 36 lines of Python from the credited upstream file ccc15s2.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 2015 S2 - Jerseys · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2015 Senior 2: Jerseys## The problem is simple.# 1. store the jersy sizes. (no tricks required)# 2. read reach request: if its a small -> satisfied#                        if sizes match -> satisfied#                        if its a M request for a L -> satisfied#                        Once a jersy has been given out, mark it as "T" (taken)## With some tweaking of the file input and converting a string to an integer using# int(s,10) I was able to get the time for the last data set under 4 seconds # with my old machine (XP Pentium 4 CPU, 3.2Ghz, 1G RAM).#  file = open("s2.6.in", "r")j = int(file.readline())a = int(file.readline()) size = []for i in range(j):    size.append (file.readline().strip()) requestsSatisfied = 0for line in file:    number = int(line[2:],10) - 1    line = line[0]    if size[number] != 'T':        if line == 'S' or \               line == size[number] or \               (line == 'M' and size[number] == 'L'):            requestsSatisfied = requestsSatisfied + 1            size[number] = 'T' print requestsSatisfied 

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 ↗