LogoLogo
  • CP Gymnasium
  • Week 3 Math / Number Theory
    • Week 3 Math / Number Theory
      • Problem 1: Tetration
      • Problem 2: Peragrams
      • Problem 3: Veci
      • Problem 4: Architecture
      • Problem 5: Joint Attack
      • Problem 6: How Many Digits?
      • Problem 7: Abstract Painting
  • Week 4 Array / Greedy
    • Week 4 Array / Greedy
      • Problem 1: Vaccine Efficacy
      • Problem 2: Frosh Week
      • Problem 3: Inquiry
      • Problem 4: Bank Queue
      • Problem 5: Log Land
  • Week 6 Sorting / Binary Search
    • Week 6 Sorting / Binary Search
      • Problem 1: Falling Apart
      • Problem 2: Synchronizing Lists
      • Problem 3: Distributing Ballot Boxes
      • Problem 4: Financial Planning
      • Problem 5: Big Boxes
  • Week 7 Dynamic Programming
    • Week 7 Dynamic Programming
      • Problem 1: Ocean's Anti-11
      • Problem 2: Batmanacci
      • Problem 3: Radio Commercials
      • Problem 4: Welcome to Code Jam (Hard)
      • Problem 5: Honeycomb Walk
  • Week 8 Graph Traversals
    • Week 8 Graph Traversals
      • Problem 1: Reachable Roads
      • Problem 2: Money Matters
      • Problem 3: Squawk Virus
      • Problem 4: Beehives
      • Problem 5: Running MoM
      • Problem 6: Amanda Lounges
Powered by GitBook
On this page
  1. Week 7 Dynamic Programming
  2. Week 7 Dynamic Programming

Problem 3: Radio Commercials

https://open.kattis.com/problems/commercials/

Given a list of profits at each hour, calculate the maximum profit that you can from some continuous sequence of commercial breaks.

First, note that at each hour,

net income = (# of students at each hour) - (price of commercial break)

At each hour, the two options are:

  1. Play the commercial.

  2. Don't play the commercial, which ends a continuous sequence of commercial breaks.

We can use an listA to keep track of the positive gain over the sequence of commercial breaks, and find the maximum possible gain.

If the current gain (sum of incomes over continuous period) is negative at a certain hour i, then including this hour in our operating time will not maximize the profit. Thus, negative gain is a criterion for not playing the commercial. We set A[i] = 0, which ends a continuous subsequence of operating hours.

Otherwise, A[i] = A[i - 1] + NetIncome[i]. This is the possible (nonnegative) gain at each hour.

The maximum possible profit will be maxiA[i]max_i {A[i]}maxi​A[i] .

PreviousProblem 2: BatmanacciNextProblem 4: Welcome to Code Jam (Hard)

Last updated 4 years ago