Search HoldMyTwist

Browse all posts

Episode 28

The Lanterns Between Two Tickets

Duck: Tickets 2 3 and 4. How many lanterns in total?

Episodes

Computing Basics

    Duck looks at five ticket cards.
    Duck moves 3 cards up from the middle. Robot points to the two ends.
    Robot observes the count.
    Robot unrolls long strip of paper across counter. Duck peeks from inside monitor.
    Duck has a lantern in each wing. Next to the screen is a lidded basket.
    Duck wheels on five lanterns; picks up another.
    Robot raises fist when last box is filled.
    Duck has the bracket above tickets that were selected.
    Duck just slid out the previous prefix card.
    Duck responds to absence of the ticket card. Robot has the card that was mistakenly removed.
    Robot draws the limit on ruler. Duck has an inclusive pair of brackets in the monitor.
    Duck shows us the extra ticket. The original strip is still on the counter.
    Duck takes the next range...and Robot keeps the answer under a cloth.
    Robot opens 6 prefix boxes, indicates 1st and 4th positions.
    Only ticket #4 is taken.
    Robot points at first prefix box. Duck puts the completed lantern-order cards in monitor.

    Episode transcript

    Count the middle orders

    Duck looks at five ticket cards.

    Diagram values: TICKET 1 2 3 4 5; COUNT 2 5 1 4 3

    Duck: Tickets 2 3 and 4. How many lanterns in total?

    Duck moves 3 cards up from the middle. Robot points to the two ends.

    Diagram values: 2..4 INCLUSIVE

    Robot: Both ends matter.

    Robot observes the count.

    Diagram values: 5 + 1 + 4 = 10

    Duck: That'll require 10 lanterns. There's another customer waiting.

    Robot unrolls long strip of paper across counter. Duck peeks from inside monitor.

    Diagram values: P[0] = 0

    Robot: Can we do this with a running total for the next range question?

    Build running totals

    Duck has a lantern in each wing. Next to the screen is a lidded basket.

    Diagram values: P[0]=0; P[1]=2

    Robot: Start from 0.

    Duck wheels on five lanterns; picks up another.

    Diagram values: P[2]=7; P[3]=8

    Duck: Five more. We have now seven. One more. Now we have eight.

    Robot raises fist when last box is filled.

    Diagram values: P: 0,2,7,8,12,15

    Robot: 4 more make 12 then 3 more make 15.

    Duck has the bracket above tickets that were selected.

    Diagram values: WANT 2..4

    Duck: That includes ticket 1 - but our customer only wants tickets 2-4.

    Subtract the earlier prefix

    Duck just slid out the previous prefix card.

    Diagram values: BEFORE P[4] = 12; T1:2 T2:5 T3:1 T4:4; REMOVE P[1] = 2; AFTER T2:5 T3:1 T4:4; P[4] − P[1]; 12 − 2 = 10; 5 + 1 + 4 = 10

    Robot: We need 10 for our order.

    Duck responds to absence of the ticket card. Robot has the card that was mistakenly removed.

    Diagram values: P[4] − P[2] = 5; THIS COVERS 3..4

    Duck: I accidentally removed ticket two!

    Robot draws the limit on ruler. Duck has an inclusive pair of brackets in the monitor.

    Diagram values: SUM i..j = P[j] − P[i−1]

    Robot: Take the ending ticket total, subtract the total just before the starting ticket.

    Duck shows us the extra ticket. The original strip is still on the counter.

    Diagram values: COUNTS UNCHANGED

    Duck: Remember to recompute prefix totals if original counts change.

    A range from the start

    Duck takes the next range...and Robot keeps the answer under a cloth.

    Diagram values: RANGE 1..3

    Duck: What two totals do we need?

    Robot opens 6 prefix boxes, indicates 1st and 4th positions.

    Diagram values: P[3] − P[0]; 8 − 0 = 8

    Robot: The first 3 tickets required 8 lanterns.

    Only ticket #4 is taken.

    Diagram values: P[4] − P[3] = 4

    Duck: For ticket four specifically, (12-8) = 4 lanterns.

    Robot points at first prefix box. Duck puts the completed lantern-order cards in monitor.

    Diagram values: P[0]=0; INCLUSIVE TICKETS

    Robot: So we can just apply this same rule to the first ticket, since we start with zero.

    The lesson

    Diagram values: P[0] = 0; P[k] = total through ticket k; Tickets i through j: P[j] − P[i−1]

    So imagine the strip as a series of pictures that we take after each ticket. Picture 4 already has the first 4 tickets in it. There are 12 lanterns. 2 of them belong to the first ticket. If we remove them, there are 10 lanterns left.

    The first blank is 0 lanterns. So Duck has something to subtract even if you start at ticket 1.

    It takes a single pass across the tickets to make the strip. From then on, it only takes two lookups and one subtraction, even if it's a massive order. So if it's a small order, you can just count up the lanterns by hand. But if people are constantly asking about the same row of tickets, then you've got a use for your strip.

    Try it

    Diagram values: TICKETS 1, 2, 3, 4; LANTERN COUNTS 6, 1, 2, 5; P[0] = 0; ORDER TICKETS 2..4, INCLUSIVE

    How many lanterns in total? What prefix total do you need to subtract?

    Answer

    Prefixes 0,6,7,9,14. Tickets 2-4 need 14-6=8 lanterns. Subtract 7 and we get 7 lanterns and miss the 1 on ticket 2.

    Sources

    NumPy documentation: cumulative sum (opens in a new tab)

    USACO Guide, Introduction to Prefix Sums (opens in a new tab)