Google Kick Start Archive — Round B 2017 problems

Overview

A. Math Encoder

Problem

Professor Math is working on a secret project and is facing a challenge where a list of numbers need to be encoded into a single number in the most efficient manner. After much research, Professor Math finds a 3 step process that can best encode the numbers:

  1. The first step is to find all possible non-empty subsets of the list of numbers and then, for each subset, find the difference between the largest and smallest numbers (that is, the largest minus the smallest) in that subset. Note that if there is only one number in a subset, it is both the largest and the smallest number in that subset. The complete set itself is also considered a subset.
  2. Then add up all the differences to get the final encoded number.
  3. As the number may be large, output the number modulo 109 + 7 (1000000007).

The professor has shared an example and its explanation below. Given a list of numbers, can you help the professor build an efficient function to compute the final encoded number?

Input

The first line of the input gives the number of test cases, T. This is followed by T test cases where each test case is defined by 2 lines:

  1. The first line gives a positive number N: the number of numbers in the list and
  2. The second line contains a list of N positive integers Ki, sorted in non-decreasing order.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the final encoded number.

Since the output can be a really big number, we only ask you to output the remainder of dividing the result by the prime 109 + 7 (1000000007).

Limits

1 ≤ T ≤ 25.
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ Ki ≤ 10000, for all i.
KiKi+1, for all i < N - 1.

Small dataset (Test set 1 - Visible)

1 ≤ N ≤ 10.

Large dataset (Test set 2 - Hidden)

1 ≤ N ≤ 10000.

Sample

Sample Input
content_copy Copied!
1
4
3 6 7 9
Sample Output
content_copy Copied!
Case #1: 44

Explanation for the sample input

  1. Find all subsets and get the difference between largest & smallest numbers:
    [3], largest-smallest = 3 - 3 = 0.
    [6], largest-smallest = 6 - 6 = 0.
    [7], largest-smallest = 7 - 7 = 0.
    [9], largest-smallest = 9 - 9 = 0.
    [3, 6], largest-smallest = 6 - 3 = 3.
    [3, 7], largest-smallest = 7 - 3 = 4.
    [3, 9], largest-smallest = 9 - 3 = 6.
    [6, 7], largest-smallest = 7 - 6 = 1.
    [6, 9], largest-smallest = 9 - 6 = 3.
    [7, 9], largest-smallest = 9 - 7 = 2.
    [3, 6, 7], largest-smallest = 7 - 3 = 4.
    [3, 6, 9], largest-smallest = 9 - 3 = 6.
    [3, 7, 9], largest-smallest = 9 - 3 = 6.
    [6, 7, 9], largest-smallest = 9 - 6 = 3.
    [3, 6, 7, 9], largest-smallest = 9 - 3 = 6.
  2. Find the sum of the differences calculated in the previous step:
    3+4+6+1+3+2+4+6+6+3+6
    = 44.
  3. Find the answer modulo 109 + 7 (1000000007):
    44 % 1000000007 = 44

Problem

There are N weighted points in a plane. Point i is at (Xi, Yi) and has weight Wi.

In this problem, we need to find a special center of these points. The center is a point (X, Y) such that the sum of max(|X-Xi|, |Y-Yi|)*Wi is minimum.

Input

The input starts with one line containing exactly one integer T, which is the number of test cases. T test cases follow.

Each test case begins with one line containing one integer N. N lines follow. Each line contains three space-separated real numbers Xi, Yi, and Wi. Xi, Yi and Wi have exactly 2 digits after the decimal point.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the sum of max(|X-Xi|, |Y-Yi|)*Wi for center (X, Y).

y will be considered correct if it is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 10.
Memory limit: 1GB.
-1000.00 ≤ Xi ≤ 1000.00.
-1000.00 ≤ Yi ≤ 1000.00.

Small dataset (Test set 1 - Visible)

Time limit: 20 seconds.
1 ≤ N ≤ 100;
Wi = 1.0, for all i.

Large dataset (Test set 2 - Hidden)

Time limit: 40 seconds.
1 ≤ N ≤ 10000;
1.0 ≤ Wi ≤ 1000.0, for all i.

Sample

Sample Input
content_copy Copied!
3
2
0.00 0.00 1.00
1.00 0.00 1.00
4
1.00 1.00 1.00
1.00 -1.00 1.00
-1.00 1.00 1.00
-1.00 -1.00 1.00
2
0.00 0.00 1.00
1.00 0.00 2.00
Sample Output
content_copy Copied!
Case #1: 1.0
Case #2: 4.0
Case #3: 1.0

C. Christmas Tree

Problem

You are given a rectangular grid with N rows and M columns. Each cell of this grid is painted with one of two colors: green and white. Your task is to find the number of green cells in the largest Christmas tree in this grid.

To define a Christmas tree, first we define a good triangle as follows:

A good triangle with top point at row R, column C and height h is an isoceles triangle consisting entirely of green cells and pointing upward. Formally, this means that: The cell (R, C) is green, and for each i from 0 to h-1 inclusive, the cells in row R+i from column C-i to column C+i are all green.

For example:

..#..
.####
#####
is a good triangle with height 3. The # cells are green and the . cells are white. Note that there is a green cell that is not part of the good triangle, even though it touches the good triangle.

..#..
.###.
####.
is NOT a good triangle, because the 5th cell in the 3rd row is white. However, there are good triangles with height 2 present.

...#.
.###.
#####.
is NOT a good triangle. However, there are good triangles with height 2 present.

A K-Christmas tree is defined as follows:

  • It contains exactly K good triangles in vertical arrangement.
  • The top cell of the i+1-th triangle must share its top edge with the bottom edge of any one of the cells at the base of the i-th triangle. This means that, if the base of the i-th triangle is at row r, from column c1 to column c2, then the top of the i+1-th triangle must be on row r+1, in a column somewhere between c1 and c2, inclusive.

For example, if K = 2:

...#...
..###..
.#####.
#######
..#....
.###...
#####..
is a valid 2-Christmas tree. Note that the height of the 2 good triangles can be different.

..#..
.###.
.#...
is also a valid 2-Christmas tree. Note that a good triangle can be of height 1 and have only one green cell.

...#...
..###..
.#####.
.......
..#....
.###...
#####..
is NOT a valid Christmas tree, because the 2nd triangle must starts from the 4-th row.

...#.
..###
.#...
###..
is NOT a valid Christmas tree, because the top of the 2nd triangle must be in a column between 3 and 5, inclusive.

You need to find the K-Christmas tree with the largest number of green cells.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of three lines:

  • The first line contains 3 space-separated integers N, M and K, where N is the number of rows of the grid, M is the number of columns of the grid and K is the number of good triangle in the desired Christmas tree.
  • The next N lines each contain exactly M characters. Each character will be either . or #, representing a white or green cell, respectively.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of green cells in the largest K-Christmas tree. If there is no K-Christmas tree, output 0.

Limits

1 ≤ T ≤ 100.
Memory limit: 1GB.
1 ≤ M ≤ 100.
1 ≤ N ≤ 100.
Each cell in the grid is either . or #.

Small dataset (Test set 1 - Visible)

Time limit: 30 seconds.
K = 1.

Large dataset (Test set 2 - Hidden)

Time limit: 80 seconds.
1 ≤ K ≤ 100.

Sample

Sample Input
content_copy Copied!
4
3 5 1
..#..
.###.
#####
3 5 1
.....
.....
.....
4 5 1
#####
#####
#####
#####
4 5 2
#####
#####
#####
#####
Sample Output
content_copy Copied!
Case #1: 9
Case #2: 0
Case #3: 9
Case #4: 10

In sample case #1, the largest 1-Christmas tree has 9 green cells:

..#..
.###.
#####

In sample case #2, there is no 1-Christmas tree.

In sample case #3, one largest 1-Christmas tree with 9 green cells is:

#####
#####
#####
#####

In sample case #4, one largest 2-Christmas tree with 10 green cells is:

#####
#####
#####
#####

Analysis — A. Math Encoder

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — B. Center

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — C. Christmas Tree

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Statistics — A. Math Encoder

Test set 1: 820 correct solutions (99.8% solve rate)

First
pwypeanut 4:01
9to0 4:24
Doju 5:13
TongWei...NJU 5:23
alexwice 5:51

Test set 2: 411 correct solutions (50.0% solve rate)

First
pwypeanut 4:01
9to0 4:24
Doju 5:13
alexwice 5:51
pps789 6:15

Statistics — B. Center

Test set 1: 190 correct solutions (23.1% solve rate)

First
pps789 22:20
rsola 25:06
saffahyjp 26:04
hank55663 27:20
kazel 27:53

Test set 2: 97 correct solutions (11.8% solve rate)

First
pps789 22:20
rsola 25:06
saffahyjp 26:04
hank55663 27:20
kazel 27:53

Statistics — C. Christmas Tree

Test set 1: 442 correct solutions (53.8% solve rate)

First
ckcz123 9:51
logan12358 14:55
likecs 19:15
Sarvagya3943 21:17
RaresC21 21:54

Test set 2: 59 correct solutions (7.2% solve rate)

First
ayaze 33:46
saffahyjp 49:18
R3g0d 54:25
pps789 57:10
ec24 60:07