Codeforce #688 (Div. 2) A. Cancel the Trains

题目

Gildong’s town has a train system that has 100 trains that travel from the bottom end to the top end and 100 trains that travel from the left end to the right end. The trains starting from each side are numbered from 1 to 100, respectively, and all trains have the same speed. Let’s take a look at the picture below.


Codeforce #688 (Div. 2) A. Cancel the Trains


The train system can be represented as coordinates on a 2D plane. The i-th train starting at the bottom end is initially at (i,0) and will be at (i,T) after T minutes, and the i-th train starting at the left end is initially at (0,i) and will be at (T,i) after T minutes. All trains arrive at their destinations after 101 minutes.

However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, n trains are scheduled to depart from the bottom end and m trains are scheduled to depart from the left end. If two trains are both at (x,y) at the same time for some x and y, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.

Input

Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤100).

Each test case contains three lines. The first line of each test case consists of two integers n and m (1≤n,m≤100) — the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.

The second line of each test case contains n integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.

The third line of each test case contains m integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between 1 and 100, inclusive.

Output

For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.

Example

input

3
1 2
1
3 4
3 2
1 3 4
2 4
9 14
2 7 16 28 33 57 59 86 99
3 9 14 19 25 26 28 35 41 59 85 87 99 100

output

0
1
3

Note

In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.

In the second case, at T=4, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.


Codeforce #688 (Div. 2) A. Cancel the Trains

题(fan)意(yi)

火车系统可以表示为2D平面上的坐标。 从底端开始的第i列火车最初位于(i,0),并且在T分钟后将位于(i,T),而从左端开始的第i列火车最初位于(0,i) 并在T分钟后到达(T,i)。 所有火车在101分钟后到达目的地。

但是,吉尔东发现,一些计划在特定时间同时出发的列车非常危险。 此时,n列火车计划从最底端出发,m列火车计划从左端出发。 如果两列火车同时在某个x和y处同时位于(x,y),它们将相互碰撞。 因此,他要您找到应该取消的最小列车数量,以防止所有此类撞车事故。

思路

由题可知火车速度相同,因此(i, 0)和(0, i)出发的火车肯定会相撞。如果从坐标原点开始给每辆火车依次编号的话,那就是编号相同的就会相撞,不同的不会相撞。因此算一下有几个编号相同的就是答案(两个相同的去掉其中一个就不会撞)

AC Code

#include <iostream>
#include <algorithm>

const int maxSize = 103;

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, m; // 计划从最底端离开的列车数量 计划从最左端离开的列车数量
        cin >> n >> m;
        int a[maxSize] = {0}; // 下标对应从最底端离开的列车编个号 0-不存在 1-存在
        for (int i=0; i<n; i++) {
            int t;
            cin >> t;
            a[t] = 1; // 标记存在该车
        }
        int cnt = 0; // 需要取消的列车数
        for (int i=0; i<m; i++) {
            int t;
            cin >> t;
            if (a[t] == 1) { // 会相撞,要取消
                cnt++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
上一篇:spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别


下一篇:spring定时任务@Component