CF1494C.1D Sokoban
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
You start on a position 0. There are n boxes, the i-th box is on a position ai. All positions of the boxes are distinct. There are also m special positions, the j-th position is bj. All the special positions are also distinct.
In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
你正在一条无限长的数轴上玩一个类似于推箱子(Sokoban)的游戏。游戏是离散的,因此你只需考虑数轴上的整数位置。
你的起始位置为 0。共有 n 个箱子,其中第 i 个箱子位于位置 ai。所有箱子的位置互不相同。此外还有 m 个特殊位置,其中第 j 个特殊位置为 bj。所有特殊位置也互不相同。
每一步中,你可以向左或向右移动一个单位距离。若你移动方向上紧邻位置有一个箱子,则你会将该箱子朝同一方向推动一格;如果该箱子前方(即再下一格)仍有另一个箱子,则该箱子也会被继续推动一格,依此类推。你不能穿过箱子,也不能将箱子拉向自己。
你可以执行任意次数(包括零次)的移动操作。你的目标是尽可能多地将箱子放置在特殊位置上。注意:部分箱子可能初始时已位于特殊位置上。
输入格式
The first line contains a single integer t (1≤t≤1000) — the number of testcases.
Then descriptions of t testcases follow.
The first line of each testcase contains two integers n and m (1≤n,m≤2⋅105) — the number of boxes and the number of special positions, respectively.
The second line of each testcase contains n distinct integers in the increasing order a1,a2,…,an (−109≤a1<a2<⋯<an≤109; ai=0) — the initial positions of the boxes.
The third line of each testcase contains m distinct integers in the increasing order b1,b2,…,bm (−109≤b1<b2<⋯<bm≤109; bi=0) — the special positions.
The sum of n over all testcases doesn't exceed 2⋅105. The sum of m over all testcases doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤1000)—— 测试用例的数量。
接下来是 t 个测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n,m≤2⋅105)—— 分别表示箱子的数量和特殊位置的数量。
每个测试用例的第二行包含 n 个严格递增的互异整数 a1,a2,…,an(−109≤a1<a2<⋯<an≤109;且对所有 i,ai=0)—— 表示箱子的初始位置。
每个测试用例的第三行包含 m 个严格递增的互异整数 b1,b2,…,bm(−109≤b1<b2<⋯<bm≤109;且对所有 i,bi=0)—— 表示特殊位置。
所有测试用例中 n 的总和不超过 2⋅105。所有测试用例中 m 的总和不超过 2⋅105。
输出格式
For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
对于每个测试用例,输出一个整数——能够放置在特殊位置上的箱子的最大数量。
输入输出样例
输入#1
5 5 6 -1 1 5 11 15 -4 -3 -2 6 7 15 2 2 -1 1 -1000000000 1000000000 2 2 -1000000000 1000000000 -1 1 3 5 -1 1 2 -2 -1 1 2 5 2 1 1 2 10
输出#1
4 2 0 3 1
说明/提示
In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position −1 and push a box to −2. At the end, the boxes are on positions [−2,6,7,11,15], respectively. Among them positions [−2,6,7,15] are special, thus, the answer is 4.
In the second testcase you can push the box from −1 to −109, then the box from 1 to 109 and obtain the answer 2.
The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
在第一个测试用例中,你可以向右移动 5:位置 1 上的箱子被推到位置 6,位置 5 上的箱子被推到位置 7。然后你可以向左移动 6,最终到达位置 −1,并将一个箱子推到位置 −2。最终,箱子的位置分别为 [−2,6,7,11,15]。其中,位置 [−2,6,7,15] 是特殊位置,因此答案为 4。
在第二个测试用例中,你可以将位置 −1 上的箱子推至 −109,再将位置 1 上的箱子推至 109,从而得到答案 2。
第三个测试用例说明你不允许拖拽箱子,因此无法将箱子移近特殊位置。
第四个测试用例中,所有箱子初始即位于特殊位置,因此你无需进行任何操作,答案仍为 3。
第五个测试用例中,特殊位置的数量少于箱子数量。你可以向右移动 8 或 9,使某个箱子到达位置 10。
输入解题思路,AI测评打分。不知道怎么写?