当前位置: 首页 > 工具软件 > Traffic Cone > 使用案例 >

Traffic

姬飞昂
2023-12-01

Avin is observing the cars at a crossroads. He finds that there are n cars running in the east-west direction with the i-th car passing the intersection at time ai . There are another m cars running in the north-south direction with the i-th car passing the intersection at time bi . If two cars passing the intersections at the same time, a traffic crash occurs. In order to achieve world peace and harmony, all the cars running in the north-south direction wait the same amount of integral time so that no two cars bump. You are asked the minimum waiting time.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 1, 000). The second line contains n distinct integers ai (1 ≤ ai ≤ 1, 000). The third line contains m distinct integers bi (1 ≤ bi ≤ 1, 000).
Output
Print a non-negative integer denoting the minimum waiting time.
Sample Input
1 1
1
1
1 2
2
1 3
Sample Output
1
0
翻译:
Avin在十字路口观察车辆。他发现有n辆车在东西方向行驶,第i辆车在ai时刻通过十字路口。还有m辆车在南北向行驶,第i辆车在时刻bi通过交叉路口。如果两辆车同时通过十字路口,就会发生交通事故。为了实现世界的和平与和谐,南北方向行驶的所有车辆都要等待相同的积分时间,这样就不会有两辆车相撞。你会被问到最小的等待时间。

分析

东西方向的车不需要等待,南北方向的车必须等待相同的时间。
所以我们要做的是:从0开始模拟,找到一个时间t,加到所有南北方向的车的通过时间上,使得所有南北方向的车不会和东西方向的车发生碰撞。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main{
	static int N=(int)1e3+1,n,m,l,r,t,area=0;
	static int []b,sum;
	static boolean []a;
	
	static FastScanner sc = new FastScanner(System.in);// 快读
	static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));// 快速输出
	
	public static void main(String[] args)throws IOException {
		Scanner sc=new Scanner(System.in);
		a=new boolean [N<<1];
		b=new int [N];
		while(sc.hasNext()) {
			n=sc.nextInt();m=sc.nextInt();
			Arrays.fill(a, false);
			for(int i=1;i<=n;i++) {
				t=sc.nextInt();
				a[t]=true;			
			}
			for(int i=1;i<=m;i++) b[i]=sc.nextInt();
			int ans=0;
			for(int i=1;i<=m;i++) {
				if(a[b[i]+ans]) {
					i=1;
					ans++;
				}
			}
			System.out.println(ans);
//			out.flush();
		}
//		out.close();
	}
	static void a(int n,int []a) {
		for(int i=1;i<=n;i++) {
			a[i]=sc.nextInt();
		}
	}
}
class FastScanner {
	BufferedReader br;
	StringTokenizer st;

	public FastScanner(InputStream in) {
		br = new BufferedReader(new InputStreamReader(in), 16384);
		eat("");
	}

	public void eat(String s) {
		st = new StringTokenizer(s);
	}

	public String nextLine() {
		try {
			return br.readLine();
		} catch (IOException e) {
			return null;
		}
	}

	public boolean hasNext() {
		while (!st.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	public String next() {
		hasNext();
		return st.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}
 类似资料:

相关阅读

相关文章

相关问答