G - Traffic

vin 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

题意:n辆车从一边经过十字路口,m辆车从另一边经过十字路口,当n方向的车经过路口时,m方向的车必须停下等待。给定车辆经过路口的时间,问m方向的车最少要等多久

题解:假设等待时间时t,当两个方向 的车同时经过路口时需要等,即b[j]+t==a[i],枚举输出最大的t即可

#include<iostream>
#include<string.h>
using namespace std;
int a[10005],b[10005];
int main()
{
    int n,m,x;
    while(cin>>n>>m)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++)
        {
            cin>>x;
            a[x]=1;
        }
        for(int i=0;i<m;i++)
          cin>>b[i];
        int t=0;
        for(int i=0;i<m;i++)
        {
            if(a[b[i]+t])//如果b[i]+t==a[j]
            {           
                t++;
                i=-1;
            }
        }
        cout<<t<<endl;
    }
    return 0;
}

 

posted @ 2019-07-23 20:59  知道了呀~  阅读(392)  评论(0编辑  收藏  举报