Mysterious Road Signs
InterviewTime limit10sMemory limit1024 MB
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 mysterious road signs, each with numbers on both sides. The -th sign (numbered from west to east) is kilometers east of Signfield. It has the number on its west-facing side and the number 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 and , measured in kilometers east of Signfield, where and are any numbers (not necessarily positive or distinct), such that every sign in the set satisfies at least one of: or .
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 , the number of test cases. Each test case begins with a line containing . Then lines follow, and the -th of them contains , , and . The signs are given in west-to-east order.
출력
For each test case, print one line in the form Case #x: y z. Here is the test case number starting from 1, is the largest size of a valid set, and is the number of valid sets of that size.
제한
- .
- for all .
- for all .
- for all .
- for all .
힌트
A single sign always forms a valid set, because and can be chosen to match it. A set that works for several and pairs still counts only once. A number on one side of a sign cannot stand in for the number on the other side.