#GL0000359. Vẻ đẹp của núi (Beauty of the Mountains)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Vẻ đẹp của núi (Beauty of the Mountains)
Beauty of the Mountains
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Consider an grid of nonnegative integer heights . A binary label indicates a mountain's type: 0 means snow-covered, while 1 means not snow-covered.
In one move, select any contiguous subgrid and add the same arbitrary integer to every height in it. The value may be negative, and heights may become negative; labels do not change. You may perform any number of moves.
Determine whether the sum of heights of type-0 cells can equal the sum of heights of type-1 cells. If a type is absent, its total is zero.
PHOJ format: Each input file has one case, without the original leading test count .
Input
- First line: .
- Next lines: nonnegative heights per line.
- Last lines: binary strings of length describing the labels.
Output
Print YES if equality is achievable; otherwise print NO.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): $1 <= n,m <= 50; 1 <= k <= min(n,m); 0 <= a_ij <= 10^5$.
- Subtask 3 (50 points): $1 <= n,m <= 500; 1 <= k <= min(n,m); 0 <= a_ij <= 10^9$.
Every binary row consists of exactly characters from 0 and 1.
Examples
Example 1
Input
2 2 2
3 4
6 7
00
00
Output
YES
Explanation
All four cells are type 0, with total height 20. Adding c=-5 to the only 2x2 square makes its total 0, matching the absent type-1 total.
Example 2
Input
2 2 2
3 4
6 8
00
00
Output
NO
Explanation
The type-0 sum is 21, and each operation changes it by a multiple of 4, so equality with zero is impossible.
Example 3
Input
1 2 1
1 2
01
Output
YES
Explanation
With k=1, each cell can be changed independently. Increasing the type-0 cell from 1 to 2 equalizes the totals.