Accel Thinking

CP Weekly Challenges

USACO 2020 December Bronze Daisy Chains Solution: Bounded Enumeration with Exact Average Checks

Walkthrough of USACO 2020 December Bronze Daisy Chains: enumerate every interval, test divisibility for an integer average, and check the average exists in the range. Python and C++ solutions, complexity, and why N=100 makes brute force the intended answer.

A row of N daisies, flower i with p_i petals. Bessie photographs every contiguous interval [i, j] — how many photos contain an “average flower”: one whose petal count equals the exact average of the interval? USACO 2020 December Bronze Problem 2, Daisy Chains, is a gentle enumeration lesson. What it teaches is not a clever optimization but something more important: read the bounds before choosing an algorithm. N ≤ 100, so there are only about 5050 intervals — “cubic” is a legal, full-marks solution here.

Where This Fits in the USACO / CP DSA Map

What to Study Next

What the Problem Is Asking

The first line gives N (1 ≤ N ≤ 100), the second N petal counts p_i (1 ≤ p_i ≤ 1000). Every interval [i, j] gets one photo. A photo has an “average flower” if some flower in the interval has exactly P petals, where P is the interval’s average. Output the number of qualifying photos.

Official sample: N=4, petals [1 1 2 3] → output 6: the 4 single-flower photos qualify; interval (1,2) averages 1 and a 1 is present; (2,4), i.e. [1,2,3], averages 2 and a 2 is present.

Two tests, both required: the average must be an integer (the sum divisible by the length), and some flower in the interval must equal it.

Where Beginners Often Get Stuck

Core Algorithm Idea

All photos = all contiguous intervals, N(N+1)/2 of them. Answer two questions per interval:

Is the sum divisible by the length?        -> total % length == 0
Does some flower equal that average?       -> average appears in petals[i..j]

Both yes → count the photo. The complexity ledger is the point: at N=100 there are ~5050 intervals; each needs one divisibility test plus at most 100 lookups, about 5×10⁵ operations in total — milliseconds in any language. That is why Bronze problems keep N small: they test whether you can write a clean enumeration, not whether you can optimize.

One detail worth keeping: compute the average, then scan the interval for it. Since p_i ≥ 1 and the average ≥ 1, you can stop at the first hit — a micro-optimization that is irrelevant at this scale.

State / Data Structure Design

Algorithm Steps

  1. Read N and the petal array.
  2. Set photos = 0.
  3. For each left from 0 to N−1:
    • Set total = 0;
    • For each right from left to N−1: add petals[right] into total; compute length; if total % length == 0, let average = total // length; if average appears in petals[left..right], increment photos.
  4. Print photos.

Sample Check with the Official Data

Verifying the sample [1 1 2 3] interval by interval (✓ = has an average flower):

Interval Content Sum Len Avg Present? Counts
(1,1) [1] 1 1 1 ✓ 1
(2,2) [1] 1 1 1 ✓ 1
(3,3) [2] 2 1 2 ✓ 1
(4,4) [3] 3 1 3 ✓ 1
(1,2) [1,1] 2 2 1 ✓ 1
(2,3) [1,2] 3 2 1.5 not integer 0
(3,4) [2,3] 5 2 2.5 not integer 0
(1,3) [1,1,2] 4 3 4/3 not integer 0
(2,4) [1,2,3] 6 3 2 ✓ 1
(1,4) [1,1,2,3] 7 4 1.75 not integer 0

Total 6, matching the official output. This table also models a dry run: hand-check it before submitting.

Python Solution

n = int(input())
petals = list(map(int, input().split()))

photos = 0
for left in range(n):
    total = 0
    for right in range(left, n):
        total += petals[right]
        length = right - left + 1
        if total % length != 0:
            continue
        average = total // length
        if average in petals[left:right + 1]:
            photos += 1

print(photos)

The outer-left, inner-right loops cover every interval; total accumulates inside the inner loop so sums are never recomputed; the divisibility test runs before the slice membership test — cheapest condition first.

C++ Solution

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> petals(n);
    for (int i = 0; i < n; i++) {
        cin >> petals[i];
    }

    int photos = 0;
    for (int left = 0; left < n; left++) {
        int total = 0;
        for (int right = left; right < n; right++) {
            total += petals[right];
            int length = right - left + 1;
            if (total % length != 0) {
                continue;
            }
            int average = total / length;
            for (int k = left; k <= right; k++) {
                if (petals[k] == average) {
                    photos += 1;
                    break;
                }
            }
        }
    }

    cout << photos << '\n';
    return 0;
}

Mirrors the Python version step by step; break on the first matching flower; integer arithmetic throughout, no floating point anywhere.

Why the Algorithm Is Correct

Complexity Analysis

Common Mistakes

  1. Forgetting single-flower intervals: (i,i) are photos too, and always qualify, contributing N.
  2. Skipping the “average must be present” test: divisibility alone is not enough.
  3. Floating-point averages: use integer division after the divisibility check.
  4. Enumeration off-by-one: right starting at 0 or missing right == left duplicates or drops intervals.
  5. Over-optimizing: reaching for sliding-window math first often writes bugs; at Bronze’s N=100, brute force is the intended solution.

Review Questions

  1. Why is O(N³) full marks here? Compute the operation count at N=100.
  2. If N became 10⁵ with the same petal bounds, would this algorithm survive? What would you change?
  3. Which of the two tests should run first, and why is the order cheaper?
  4. Why does a single-flower interval always qualify? What does that say about the definition of average?
  5. Hand-compute the answer for [1 2 1 2], then verify by running. (Hint: count singletons first, then pairs.)

Official Sources