CF1494C 1D Sokoban

C. 1D Sokoban
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 00. There are nn boxes, the ii-th box is on a position aiai. All positions of the boxes are distinct. There are also mm special positions, the jj-th position is bjbj. 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.

Input

The first line contains a single integer tt (1t10001≤t≤1000) — the number of testcases.

Then descriptions of tt testcases follow.

The first line of each testcase contains two integers nn and mm (1n,m21051≤n,m≤2⋅105) — the number of boxes and the number of special positions, respectively.

The second line of each testcase contains nn distinct integers in the increasing order a1,a2,,ana1,a2,…,an (109a1<a2<<an109−109≤a1<a2<⋯<an≤109; ai0ai≠0) — the initial positions of the boxes.

The third line of each testcase contains mm distinct integers in the increasing order b1,b2,,bmb1,b2,…,bm (109b1<b2<<bm109−109≤b1<b2<⋯<bm≤109; bi0bi≠0) — the special positions.

The sum of nn over all testcases doesn't exceed 21052⋅105. The sum of mm over all testcases doesn't exceed 21052⋅105.

Output

For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.

Example
input
Copy
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
output
Copy
4
2
0
3
1
Note

In the first testcase you can go 55 to the right: the box on position 11 gets pushed to position 66 and the box on position 55 gets pushed to position 77. Then you can go 66 to the left to end up on position 1−1 and push a box to 2−2. At the end, the boxes are on positions [2,6,7,11,15][−2,6,7,11,15], respectively. Among them positions [2,6,7,15][−2,6,7,15] are special, thus, the answer is 44.

In the second testcase you can push the box from 1−1 to 109−109, then the box from 11 to 109109 and obtain the answer 22.

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 33.

In the fifth testcase there are fewer special positions than boxes. You can move either 88 or 99 to the right to have some box on position 1010.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 300001;
 5 map<int, int> mp;
 6 int input[N], inask[N];
 7 int a[N], b[N];
 8 
 9 int solve(int lena, int lenb){
10     int count = 0;
11     mp.clear();
12     for(int i = 1; i <= lena; i++){
13         mp[a[i]] = 1;
14     }
15     for(int i = 1; i <= lenb; i++)
16         if(mp[b[i]]) count++;
17     int res = count;
18     int ans = res;
19     for(int i = 1; i <= lenb; i++){
20         if(mp[b[i]]){
21             res--;
22             continue;
23         }
24         int pp = upper_bound(a+1, a+1+lena, b[i])-a-1; // pp表示连在一起的箱子有多少个
25         int qq = upper_bound(b+1, b+lenb+1, b[i]-pp)-b; // qq表示在一坨箱子最后一个前面的特殊点的位置序号(第几个)
26         ans = max(ans, res+i-qq+1);
27     }
28     return ans;
29 }
30 
31 int main()
32 {
33     int T;
34     cin >> T;
35     while(T--){
36         int n, m;
37         int lena = 0, lenb = 0;
38         scanf("%d%d", &n, &m);
39         for(int i = 1; i <= n; i++){
40             scanf("%d", &input[i]);
41             if(input[i] >= 0)
42                 a[++lena] = input[i];
43         }
44         for(int i = 1; i <= m; i++){
45             scanf("%d", &inask[i]);
46             if(inask[i] >= 0)
47                 b[++lenb] = inask[i];
48         }
49         int ans = solve(lena, lenb);
50         lena = lenb = 0;
51         for(int i = n; i >= 1; i--){
52             if(input[i] < 0) a[++lena] = -input[i];
53         }
54         for(int i=m; i>=1; i--){
55             if(inask[i] < 0) b[++lenb] = -inask[i];
56         }
57         ans += solve(lena, lenb);
58 
59         cout << ans << endl;
60 
61     }
62 
63     return 0;
64 }
原文地址:https://www.cnblogs.com/sineagle/p/14512649.html