Image Correction Task
Time limit3sMemory limit1024 MB
Given a grid of sharpness values, correct at most K cells to the maximum X so the largest difference between adjacent cells is as small as possible.
- Level
Hard8 of 10
- Topics
- Binary search, Graph
- Solved
- No attempts yet
Problem
Hyundai Mobis is a global auto parts company with strong research in smart mobility, including autonomous driving, connectivity, and electrification. The company develops software for autonomous driving and driver assistance, including software that processes images from vehicle cameras. Images can be unclear at night or in bad weather, so correcting images with software after capture matters.

An image from a vehicle camera can be split into rows and columns, giving regions in total. The clarity of the image in each region is an integer called sharpness.
Correcting one region sets its sharpness to the maximum sharpness . Ideally every region would be corrected, but the driver waits too long if correction takes time, so at most regions are corrected.
If two adjacent regions differ a lot in sharpness, the image looks unnatural to the driver. Two regions are adjacent if they share an edge. The largest sharpness difference between adjacent regions should be as small as possible.
Find the minimum possible value of the largest sharpness difference between adjacent regions after correcting at most regions.
Input
The first line contains , , , and , separated by spaces. Each of the next lines contains integers , separated by spaces. is the sharpness of the region in row from the top and column from the left.
Output
Print the minimum possible value of the largest sharpness difference between adjacent regions after correction.