#P13140. [GCJ 2018 #1B] Mysterious Road Signs

    ID: 15005 远端评测题 10000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP2018双指针 two-pointerGoogle Code Jam

[GCJ 2018 #1B] Mysterious Road Signs

题目描述

The town of Signfield is on a perfectly straight and infinitely long road running from west to east. Along that road, there is a sequence of S\mathbf{S} mysterious road signs with numbers on both sides. The ii-th sign (numbered in order from west to east) is at a point Di\mathbf{D}_{\mathbf{i}} kilometers east of Signfield, and has a number Ai\mathbf{A}_{\mathbf{i}} on the west-facing side and a number Bi\mathbf{B}_{\mathbf{i}} on the east-facing side.

Nobody in Signfield knows what these signs are trying to say. You think that the numbers on the west sides of the signs are intended for drivers traveling east, and that they represent distances to some particular destination. Similarly, you think that the numbers on the east sides of the signs are for drivers traveling west, and that they represent distances to some particular destination. You suspect that not all of the signs may be consistent with this theory, though.

To start testing your theory, you are interested in finding valid sets of signs that obey the following rules:

  • The set is a contiguous subsequence of the sequence of all road signs. (The entire sequence also counts as a contiguous subsequence.)
  • There exist locations M\mathrm{M} and N\mathrm{N} kilometers east of Signfield, where M\mathrm{M} and N\mathrm{N} are (not necessarily positive and not necessarily distinct) numbers, such that for every sign in that set, at least one of the following is true:
    • $\mathbf{D}_{\mathbf{i}}+\mathbf{A}_{\mathbf{i}}=\mathbf{M}$.
    • $\mathbf{D}_{\mathbf{i}}-\mathbf{B}_{\mathbf{i}}=\mathbf{N}$.

What is the largest possible number of signs in a valid set as described above, and how many different valid sets of that size are there?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each begins with one line containing one integer S\mathbf{S} : the number of road signs. Then, S\mathbf{S} more lines follow. The ii-th of these lines represents the ii-th sign (in west-to-east order), and contains three integers Di,Ai\mathbf{D}_{\mathbf{i}}, \mathbf{A}_{\mathbf{i}}, and Bi\mathbf{B}_{\mathbf{i}} : the sign's distance (in kilometers) east of Signfield, the number on its west side, and the number on its east side.

输出格式

For each test case, output one line containing Case #x: y z, where xx is the test case number (starting from 1), and yy and zz are the largest possible number of signs in a valid set and the number of valid sets of that size, as described in the problem statement.

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
Case #1: 1 1
Case #2: 3 2
Case #3: 5 1

提示

Sample Explanation

In Sample Case #1, there is only one sign. If we choose just that sign as our set, there are many possible values of M\mathrm{M} and N\mathrm{N} that work - for example:

  • M=2\mathrm{M}=2 and N=0\mathrm{N}=0
  • M=1\mathrm{M}=1 and N=0\mathrm{N}=0 (remember that each sign only needs to be correct for one of its values; also, M\mathrm{M} and N\mathrm{N} might be in the same place as one or more signs, or Signfield itself)
  • M=2\mathrm{M}=2 and N=12345\mathrm{N}=-12345 ( N\mathrm{N} might be west of Signfield)
  • M=0\mathrm{M}=0 and N=0\mathrm{N}=0 ( M\mathrm{M} and N\mathrm{N} are not necessarily distinct)
  • M=2\mathrm{M}=2 and N=3\mathrm{N}=3 ( N\mathrm{N} might be east of M\mathrm{M})

So, the set consisting of just that one sign is valid. That is the only set of that length, so the answer is 11 .

In Sample Case #2, note that the first, second, fourth, and fifth signs would be consistent with M=9\mathrm{M}=9 and N=1\mathrm{N}=-1, but they do not form a contiguous subsequence. (The 1 on the back of the third sign cannot be used as if it were on the front.) As it turns out, there is no valid set of four signs. There are two different valid sets of three signs. Note that although there are two different M/N\mathrm{M} / \mathrm{N} pairs that make the second set of three signs valid, that set counts only once:

  • the first, second, and third signs, with M=9\mathrm{M}=9 and N=7\mathrm{N}=7
  • the third, fourth, and fifth signs, with M=18\mathrm{M}=18 and N=1\mathrm{N}=-1 or with M=22\mathrm{M}=22 and N=7\mathrm{N}=7

In Sample Case #3, the entire sequence is a valid set, with M=4\mathrm{M}=4 and N=2\mathrm{N}=2.

Limits

  • 1T601 \leqslant \mathbf{T} \leqslant 60.
  • $1 \leqslant \mathbf{D}_{\mathbf{i}} \leqslant 10^{6}$, for all ii.
  • Di<Dj\mathbf{D}_{\mathbf{i}}<\mathbf{D}_{\mathbf{j}}, for all i<ji<j.
  • $1 \leqslant \mathbf{A}_{\mathbf{i}} \leqslant 10^{6}$, for all ii.
  • $1 \leqslant \mathbf{B}_{\mathbf{i}} \leqslant 10^{6}$, for all ii.

Test set 1 (10 Pts, Visible)

  • 1S1001 \leqslant \mathbf{S} \leqslant 100 for all test cases.

Test set 2 (20 Pts, Hidden)

  • 1S1001 \leqslant \mathbf{S} \leqslant 100 for all but 3 test cases.
  • S=105\mathbf{S}=10^{5} for 3 test cases.