Connection Status:
Competition Arena > RectangleArea
Member SRM 505 · 2010-11-01 · by ivan_metelsky · Graph Theory, Simple Math
Class Name: RectangleArea
Return Type: int
Method Name: minimumQueries
Arg Types: (vector<string>)
Problem Statement

Problem Statement

Your friend has drawn a rectangle on the coordinate plane consisting of points (x, y) such that 0 <= x <= W, 0 <= y <= H, where W and H are positive numbers (not necessarily integer). Then she has divided it into a grid of NxM smaller rectangles. In order to do it, she has chosen numbers (X[0], X[1], ..., X[N]) and (Y[0], Y[1], ..., Y[M]) such that 0 = X[0] < X[1] < ... < X[N] = W and 0 = Y[0] < Y[1] < ... < Y[M] = H. The small rectangle in row i, column j of the grid consists of points (x, y) such that Y[i] <= y <= Y[i+1] and X[j] <= x <= X[j+1]. Note that rows are numbered from 0 to M-1 and columns are numbered from 0 to N-1.

Now, you and your friend play the following game. Initially, you only know the values of N and M, but do not know the values of W, H, X and Y. Your goal is to determine the area of the initial large rectangle, i.e., W * H, but since you don't know W and H, it's not so easy to do. In order to achieve the goal, you can ask questions to your friend. Each question is of the form: "What is the area of the small rectangle in row i, column j?" Your friend always gives true answers, i.e., she responds with the value of (Y[i+1] - Y[i]) * (X[j+1] - X[j]).

You have already asked some questions, the information about them is contained in a String[] known. It contains M elements, each element consists of N characters. The j-th character in i-th element is 'Y' if you already know the area of the small rectangle in row i, column j, and it is 'N' if you haven't yet asked the question about this small rectangle.

Return the minimum number of additional questions that you need to ask in order to determine the area of the initial large rectangle. If you can determine the area without asking additional questions, return 0.

Notes

  • Your goal is always achievable. For example, if you know the areas of all small rectangles, you can find the area of the large rectangle as the sum of areas of all small rectangles.

Constraints

  • known will contain between 1 and 50 elements, inclusive.
  • Each element of known will contain between 1 and 50 characters, inclusive.
  • All elements of known will contain the same number of characters.
  • Each character in known will be 'Y' or 'N'.
Examples
0)
{"NN",
 "NN"}
Returns: 3

Suppose that heights of rows 0 and 1 are R0 and R1 and widths of columns 0 and 1 are C0 and C1. You can ask 3 questions about rectangle at row 0, column 0 (suppose the answer is A00), at row 0, column 1 (answer is A01) and at row 1, column 0 (answer is A10). We then have the following equations: R0 * C0 = A00 R0 * C1 = A01 R1 * C0 = A10 After several transformations: C0 = A00 / R0 C1 = A01 / R0 R1 = A10 / C0 = A10 / (A00 / R0) = (A10 / A00) * R0 The total area can then be calculated as follows: W * H = (R0 + R1) * (C0 + C1) = = (R0 + (A10 / A00) * R0) * (A00 / R0 + A01 / R0) = = R0 * (1 + A10 / A00) * (1 / R0) * (A00 + A01) = = (1 + A10 / A00) * (A00 + A01) So, 3 questions are enough to find the area of the large rectangle. It is impossible to achieve this goal with less than 3 questions.

1)
{"YNY",
 "NYN",
 "YNY"}
Returns: 1

Knowing the contents of any single additional small rectangle is enough to find the area of the large rectangle.

2)
{"YY",
 "YY",
 "YY",
 "YY"}
Returns: 0

You already know everything in this test case, so no additional questions are needed.

3)
{"NNNNNNNNNN"}
Returns: 10

Here you need to ask questions about all small rectangles.

4)
{"NNYYYNN",
 "NNNNNYN",
 "YYNNNNY",
 "NNNYNNN",
 "YYNNNNY"}
Returns: 2

Submissions are judged against all 153 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class RectangleArea with a public method int minimumQueries(vector<string> known) · 153 test cases · 2 s / 256 MB per case

Submitting as anonymous