Problem solution · Python

CCC 2014 S4 - Tinted Glass Window

CCC 2014 S4 - Tinted Glass Window: a Python 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
89 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 2014 S4 - Tinted Glass Window, 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

  • 89 lines of Python from the credited upstream file ccc14s4.py.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 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 2014 S4 - Tinted Glass Window · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2014 S4: Tinted Glass Window## This code is by Calvin Liu of Glenforest Secondary School# (Yikuan (Timothy) Li also suggested a similar approach.) ## This algorithm uses a "Sweep-Line".# I will let Calvin explain:#   This performs coordinate compression on the Y-axis #   [that means only using the y coordinates of the rectangles]#   and runs a sweep line along the X-axis.#   Every rectangle is separated into two vertical line segments,#   an incoming edge from the left and an outgoing edge to the right.#   The value of a rectangle is added when the sweep line encounters#   the incoming edge and subtracted when it encounters the outgoing edge.#   The final answer accumulates through the process.## Here is a trace of Calvin's program, with the example given with the question #  line= [-1000000000, [11, 11, 15, 1], [12, 12, 13, 1], [13, 8, 17, 2],#                      [14, 8, 17, -2], [17, 8, 17, 1], [18, 8, 17, -1],#                      [19, 12, 13, -1], [20, 11, 15, -1]]##  After removing duplicates and sorting:#  segy= [-1000000000, 8, 11, 12, 13, 15, 17]##  coordinate compression and hash table creation#  findy= {8: 1, 11: 2, 12: 3, 13: 4, 15: 5, 17: 6}##  initialize the yaxis (keeps track of the total tint)#  yaxis= [0, 0, 0, 0, 0, 0, 0, 0]##  in the main loop#    i= 1    yaxis= [0, 0, 1, 1, 1, 0, 0, 0]#    i= 2    yaxis= [0, 0, 1, 2, 1, 0, 0, 0]#    i= 3    yaxis= [0, 2, 3, 4, 3, 2, 0, 0]#    i= 4    j= 2    ans= 1#    i= 4    j= 3    ans= 2#    i= 4    j= 4    ans= 4#    i= 4    yaxis= [0, 0, 1, 2, 1, 0, 0, 0]#    i= 5    yaxis= [0, 1, 2, 3, 2, 1, 0, 0]#    i= 6    j= 3    ans= 5#    i= 6    yaxis= [0, 0, 1, 2, 1, 0, 0, 0]#    i= 7    yaxis= [0, 0, 1, 1, 1, 0, 0, 0]#    i= 8    yaxis= [0, 0, 0, 0, 0, 0, 0, 0]## technically test data set 9 runs in 6.7 seconds and# test data set 10 runs in 5.7 seconds on my old Pentium 4 with 3.2 GHZ CPU  file = open ("s4.15.in", "r")n = int(file.readline())t = int(file.readline()) #-1000000000 is small enough to be at index 0 after sortingline = [-1000000000]#-1000000000 is just a place holdersegy = [-1000000000]    for i in xrange(n):    x1,y1,x2,y2,val=map(int,file.readline().split())    line.append([x1,y1,y2,val])        #Incoming side    line.append([x2,y1,y2,-val])       #Outgoing side    segy.append(y1)    segy.append(y2)    line.sort() #Remove duplicates and sortsegy = list(set(segy))    segy.sort() #Coordinate Compression with a hash tablefindy = {}     for i in xrange(1, len(segy)):    findy[segy[i]] = i    yaxis=[0 for i in xrange(len(segy) + 1)] ans=0 #Sweep Line loopfor i in xrange(1, len(line)):             for j in xrange(1, len(segy)):        if yaxis[j] >= t:            ans += (segy[j + 1] - segy[j]) * (line[i][0] - line[i - 1][0])    for j in xrange(findy[line[i][1]], findy[line[i][2]]):        yaxis[j] += line[i][3]        print ans 

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 ↗