Under construction

Still being built. Things may change

Mysterious Road Signs

Interview

Time limit10sMemory limit1024 MB

Summary
Find the longest contiguous run of signs where each sign matches one shared west-facing value or one shared east-facing value, and count such runs.
Level

Medium6 of 10

Topics
Sliding window, Two pointers, Hash map
Solved
No attempts yet

문제

The town of Signfield sits on a perfectly straight, infinitely long road that runs west to east. Along the road is a sequence of SS mysterious road signs, each with numbers on both sides. The ii-th sign (numbered from west to east) is DiD_i kilometers east of Signfield. It has the number AiA_i on its west-facing side and the number BiB_i on its east-facing side.

Nobody in Signfield knows what the signs mean. You suspect that the west-facing numbers are distances to a destination for drivers traveling east, and that the east-facing numbers are distances to a destination for drivers traveling west. Some signs may not fit this theory.

A set of signs is valid if it satisfies both of these rules:

  • The set is a contiguous subsequence of all the signs. The entire sequence also counts.
  • There exist locations MM and NN, measured in kilometers east of Signfield, where MM and NN are any numbers (not necessarily positive or distinct), such that every sign in the set satisfies at least one of: Di+Ai=MD_i + A_i = M or Di−Bi=ND_i - B_i = N.

Find the largest possible number of signs in a valid set, and how many different valid sets of that size exist.

입력

The first line contains TT, the number of test cases. Each test case begins with a line containing SS. Then SS lines follow, and the ii-th of them contains DiD_i, AiA_i, and BiB_i. The signs are given in west-to-east order.

출력

For each test case, print one line in the form Case #x: y z. Here xx is the test case number starting from 1, yy is the largest size of a valid set, and zz is the number of valid sets of that size.

제한

  • 1≤T≤601 \le T \le 60.
  • 1≤Di≤1061 \le D_i \le 10^6 for all ii.
  • Di<DjD_i < D_j for all i<ji < j.
  • 1≤Ai≤1061 \le A_i \le 10^6 for all ii.
  • 1≤Bi≤1061 \le B_i \le 10^6 for all ii.

힌트

A single sign always forms a valid set, because MM and NN can be chosen to match it. A set that works for several MM and NN pairs still counts only once. A number on one side of a sign cannot stand in for the number on the other side.

Examples1

  1. Example 1

    Input
    3
    1
    1 1 1
    5
    2 7 12
    6 3 11
    8 10 1
    11 11 12
    13 9 14
    5
    1 3 3
    2 2 2
    3 1 1
    4 2 2
    5 3 3
    
    Expected output
    Case #1: 1 1
    Case #2: 3 2
    Case #3: 5 1