首页 > 其他 > 详细

G - Traffic

时间:2019-07-23 21:10:30      阅读:99      评论:0      收藏:0      [点我收藏+]

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;
}

 

G - Traffic

原文:https://www.cnblogs.com/-citywall123/p/11234432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!