Fair Photography
Time limit1sMemory limit128 MB
After sorting cows by position, find the widest contiguous group containing at least K breeds with each present breed appearing equally often.
- Level
Medium7 of 10
- Topics
- Prefix sum, Hash map
- Solved
- No attempts yet
Problem
FJ has cows () standing along a long one-dimensional fence. Cow stands at position (integer in ) and has breed (). No two cows share a position.
FJ wants a photo of a contiguous interval of cows. Every breed that appears in the photo must appear the same number of times (for example, 27 of breed 1 and 27 of breed 3 is fine, but 9 of breed 1 and 10 of breed 3 is not). At least breeds () must appear.
Find the maximum photo size, defined as the difference between the largest and smallest positions among cows in the photo. If no valid photo exists, output .
Input
- Line 1: and .
- Next lines: and .
Output
One integer: the maximum fair photo size, or if none exists.
Hint
Sort by position, then check each contiguous interval for equal per-breed counts.