#P13219. [GCJ 2015 #1B] Noisy Neighbors

[GCJ 2015 #1B] Noisy Neighbors

题目描述

You are a landlord who owns a building that is an R×CR \times C grid of apartments; each apartment is a unit square cell with four walls. You want to rent out NN of these apartments to tenants, with exactly one tenant per apartment, and leave the others empty. Unfortunately, all of your potential tenants are noisy, so whenever any two occupied apartments share a wall (and not just a corner), this will add one point of unhappiness to the building. For example, a 2×22 \times 2 building in which every apartment is occupied has four walls that are shared by neighboring tenants, and so the building's unhappiness score is 44.

If you place your NN tenants optimally, what is the minimum unhappiness value for your building?

输入格式

The first line of the input gives the number of test cases, TT. TT lines follow; each contains three space-separated integers: RR, CC, and NN.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the test case number (starting from 11) and yy is the minimum possible unhappiness for the building.

4
2 3 6
4 1 2
3 3 8
5 2 0
Case #1: 7
Case #2: 0
Case #3: 8
Case #4: 0

提示

Sample Explanation

In Case #1, every room is occupied by a tenant and all seven internal walls have tenants on either side.

In Case #2, there are various ways to place the two tenants so that they do not share a wall. One is illustrated below.

In Case #3, the optimal strategy is to place the eight tenants in a ring, leaving the middle apartment unoccupied.

Here are illustrations of sample cases 1-3. Each red wall adds a point of unhappiness.

Sample Explanation

  • 1T10001 \leq T \leq 1000.
  • 0NR×C0 \leq N \leq R \times C.

Small dataset(12 Pts)

  • Time limit: 240 5 seconds.
  • 1R×C161 \leq R \times C \leq 16.

Large dataset(15 Pts)

  • Time limit: 480 10 seconds.
  • 1R×C100001 \leq R \times C \leq 10000.