#P2954. [USACO09OPEN] Grazing2 S
[USACO09OPEN] Grazing2 S
Description
Farmer John has cows, numbered , where . His newly painted barn has stalls in a single line, numbered , where . Each pair of adjacent stalls is at distance .
The cows have already gone to rest; cow is currently in stall . Since cows get grumpy when they are too close together, Farmer John wants to move them so they are as spread out as possible and with similar gaps.
Let (integer division). Farmer John wants the distances between adjacent cows to each differ from by at most , and he also wants to maximize the number of these distances that are exactly equal to .
For example, with cows and stalls, placements and are allowed, but and are not allowed.
Help Farmer John spread the cows as efficiently as possible by computing the minimum total distance the cows must move to achieve such spacing. Ignore the distance for entering or leaving a stall.
Input Format
- Line 1: Two space-separated integers and .
- Lines : Line contains a single integer .
Output Format
- Line 1: A single integer — the minimum total distance the cows must travel. This value is guaranteed to be less than .
5 10
2
8
1
3
9
4
Hint
1 2 3 4 5 6 7 8 9 10 Cow Locs | A | B | C | . | . | . | . | D | E | . |
Cows move from stall 2 to 3, 3 to 5, and 9 to 10. The total distance moved is 1 + 2 + 1 = 4. The final positions of the cows are in stalls 1, 3, 5, 8, and 10.
1 2 3 4 5 6 7 8 9 10 Init Stall | A | B | C | . | . | . | . | D | E | . | Final Stall | A | . | B | . | C | . | . | D | . | E | Distance moved | 0 | . | 1 | . | 2 | . | . | 0 | . | 1 |
Translated by ChatGPT 5
京公网安备 11011102002149号