Population Migration (APCS 2020-10 Intermediate)

Points 100 1.0s 256M

Several cities are placed on an \(R\)-row, \(C\)-column grid. Position \((r,c)\) is a city when its cell contains a nonnegative integer representing its current population; a value of \(-1\) means that the position is not a city.

Two cities are adjacent only when their cells share an edge. Thus, only the four directions up, down, left, and right are considered; diagonal cells are not adjacent.

Migration on each day follows these rules:

  1. Suppose a city has population \(p\) at the beginning of the day. Let \(q=\left\lfloor p/k\right\rfloor\).
  2. The city sends \(q\) people to each adjacent city. No migration occurs toward a direction outside the grid or toward a cell containing \(-1\), and no population is deducted for that direction.
  3. Every city computes its outgoing amount from its population at the beginning of that day, and all migrations happen simultaneously. People received during a day cannot migrate again until the next day.

Simulate \(m\) days and report the minimum and maximum city populations after day \(m\) has finished.

The following newly drawn diagram illustrates one day of the sample. A number beside an arrow is the number of people migrating in that direction; a crossed cell is not a city.

For example, the upper-left city begins with \(10\) people, so it sends \(\lfloor10/4\rfloor=2\) people to the right and \(2\) downward. At the same time, it receives \(\lfloor5/4\rfloor=1\) person from below. Its next-day population is therefore \(10-2-2+1=7\).

Input

The first line contains four positive integers \(R,C,k,m\).

The next \(R\) lines each contain \(C\) integers. The integer \(a_{r,c}\) in row \(r\), column \(c\) has the following meaning:

  • \(a_{r,c}=-1\) means that the position is not a city.
  • \(a_{r,c}\ge0\) is that city's population before migration begins on day \(1\).

Constraints

  • \(1\le R,C,m\le50\)
  • \(4\le k\le50\)
  • \(-1\le a_{r,c}\le100\)
  • The grid contains at least one city.
  • All input values are integers.

Output

On the first line, print the minimum city population after \(m\) days.

On the second line, print the maximum city population after \(m\) days.

Scoring

The sample is judged but carries no points. There are exactly \(20\) scored groups, each worth \(5\) points:

  • Groups \(1\)–\(4\) (\(20\) points): \(R=1\) and \(m=1\).
  • Groups \(5\)–\(10\) (\(30\) points): \(R=1\).
  • Groups \(11\)–\(20\) (\(50\) points): no additional constraints.

Sample Input

2 3 4 1
10 2 -1
5 -1 2

Sample Output

2
7

Sample Explanation

After one day, the four city populations are \(7,4,6,2\). Therefore, the minimum is \(2\) and the maximum is \(7\).

Source

APCS October 2020 implementation problem 2; rules based on ZeroJudge f313 “Population Migration” and Wang Yizhe's explanation. The statements and explanatory figure were newly prepared for AACPOJ.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.