The Lazy Cow
Time limit1sMemory limit128 MB
Choose a start point to maximize the total grass of patches within Manhattan distance K of it.
- Level
Medium7 of 10
- Topics
- Sliding window, Sorting, Segment tree, Geometry
- Solved
- No attempts yet
Problem
On a hot summer day, Bessie the cow feels lazy. She wants to pick a starting point in her field so that she can reach as much grass as possible within a short walk.
The field has grass patches (). Patch holds units of grass () at a distinct point (). Bessie chooses one point in the field as her start (it may coincide with a patch or use non-integer coordinates). She wants the maximum total grass within Manhattan distance of that point ().
Each step moves 1 unit north, south, east, or west. For example, to needs 5 steps. A step may be split: half a unit north plus half a unit east still counts as one step.
Input
- Line 1: integers and
- Next lines: , , for each patch
Output
Print one integer: the maximum grass Bessie can reach within steps when she picks the best start.
Hint
Rotate coordinates to . A Manhattan ball becomes a box with and . Sort by , slide a window of width at most , and within each window slide on the same way.