Ploughing
Time limit1sMemory limit128 MB
Given an m by n grid of tile difficulties, repeatedly remove a full strip of width 1 from any edge as long as the strip sum is at most k, and minimize the number of strips that remove every tile.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Two pointers, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
Byteasar the farmer wants to plough his rectangular field. The field is ploughed one slice at a time. Each slice is a full strip of width taken from one of the four edges of the not-yet-ploughed region, and after every slice the remaining region is still a rectangle. This repeats until the whole field is ploughed.
Byteasar has only one weak horse. Once the horse starts a slice, it cannot stop until that slice is finished; it may rest only between slices. Each tile has a non-negative integer ploughing difficulty. The field consists of unit tiles, and the tile in column , row (where and ) has difficulty . For any slice, the sum of the difficulties of its tiles must not exceed a constant ; otherwise the horse collapses from exhaustion, so every slice must have a difficulty sum of at most .
Before each slice Byteasar chooses which edge to plough so that no slice exceeds , and he wants to plough the entire field using as few slices as possible.
Write a program that reads , , and the difficulty coefficients, and outputs the minimum number of slices needed to plough the whole field.
Input
The first line contains three positive integers , , and , separated by single spaces (, ). Each of the next lines gives the ploughing-difficulty coefficients: line contains , separated by single spaces ().
Output
Output a single integer: the minimum number of slices needed to plough the whole field while satisfying the rule above. It is guaranteed that the field can always be ploughed.
Hint

The illustration above shows one optimal way to plough the field from the example.