Codeforces Round #688 (Div. 2) ------ 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.
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.

题意

有n列火车下往上开,有m列火车冲做往右开,如果他们同时行驶到同意位置,那么便会相撞,求最少需要取消几列火车,才可以避免相撞
在这里插入图片描述

题解

同时发车,当他们在编号相同的轨道时,假设轨道编号为x,那么x时间后便会相撞,所以取消这2列火车中的一列即可。即找出n数组和m数组里面元素相同的组数。

AC代码

#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
void closeStd() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

int main()
{
	closeStd();
	int t; cin >> t;
	while (t--) {
		int n, m; cin >> n >> m;
		map<int, int>mp;
		for (int i = 0; i < n; i++) {
			int x;  cin >> x;
			mp[x] = 1;
		}
		int num = 0;
		for (int i = 0; i < m; i++) {
			int x; cin >> x;
			if (mp[x])
				num++;
		}
		cout << num << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值