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
- Level: USACO Bronze, Difficulty Band L1
- Pattern: Bounded Enumeration + Exact Average Check
- Prerequisite: nested loops, the modulo operator
%, interval sums (Python basics: input, operators, and conditions, Python list fundamentals) - What this teaches: estimate the work from the stated bounds — 5050 intervals × 100 checks ≈ 5×10⁵, so cubic complexity still scores full marks; correctness first, elegance second
- Where it appears again: the USACO Bronze simulation line (unit B2-00), the Mixing Milk sibling, and as the mirror image of Nailed It! — there N=10⁶ forced a move to the value domain, here N=100 allows plain enumeration
What to Study Next
- Related CP Weekly problems: USACO 2018 December Bronze Mixing Milk, CCC 2017 J5/S3 Nailed It! (the value-domain counterpart when N is huge)
- Related Python / C++ foundations: Python Basics: Input, Operators, and Conditions, Python Lists: append() vs +
- Related DSA note: brute force and complexity intro (planned)
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
- Overthinking: seeing “average” triggers prefix-sum optimizations, sliding windows, or math — and traps you. N=100; a correct brute force is already full marks.
- Skipping the “average must be present” test: counting on
total % length == 0alone. Averages like 4 in the interval [2,4,6] do not qualify, because no 4 is in the photo. - Floating-point averages: comparing with
total / lengthinvites precision bugs (especially in C++). Test divisibility first, then use integer division. - Off-by-one enumeration: left from 0 to N−1, right from left to N−1; single-flower intervals (i,i) count too.
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
total: the sum of [left, right], updated in O(1) as right advances.length = right - left + 1: the interval size.photos: the qualifying-photo counter.
Algorithm Steps
- Read N and the petal array.
- Set
photos = 0. - 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; iftotal % length == 0, let average = total // length; if average appears inpetals[left..right], increment photos.
- Set
- 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
- Intervals are enumerated exactly once: left runs 0..N−1 and right runs left..N−1, generating each of the N(N+1)/2 contiguous intervals exactly once.
- The divisibility test is exact: total % length == 0 holds if and only if the average is an integer; a fractional average can never equal a petal count, which is an integer.
- The membership test matches the definition: some petals[k] == average is exactly the statement “the photo contains an average flower”; stopping at the first hit does not change correctness.
- The count is correct: each interval is judged independently and contributes one when both tests pass, so photos equals the number of qualifying photos.
Complexity Analysis
- Time O(N³) in the worst case (O(N²) intervals × O(N) membership scan); at N=100 that is about 5×10⁵ operations — measured in milliseconds for both languages.
- Space O(N): the petal array only.
- Contrast for learning: CCC’s Nailed It! pushes N to 10⁶ and brute force dies instantly — “read the bounds first” is only half the lesson until you have seen both problems.
Common Mistakes
- Forgetting single-flower intervals: (i,i) are photos too, and always qualify, contributing N.
- Skipping the “average must be present” test: divisibility alone is not enough.
- Floating-point averages: use integer division after the divisibility check.
- Enumeration off-by-one: right starting at 0 or missing right == left duplicates or drops intervals.
- Over-optimizing: reaching for sliding-window math first often writes bugs; at Bronze’s N=100, brute force is the intended solution.
Review Questions
- Why is O(N³) full marks here? Compute the operation count at N=100.
- If N became 10⁵ with the same petal bounds, would this algorithm survive? What would you change?
- Which of the two tests should run first, and why is the order cheaper?
- Why does a single-flower interval always qualify? What does that say about the definition of average?
- Hand-compute the answer for
[1 2 1 2], then verify by running. (Hint: count singletons first, then pairs.)
Official Sources
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
