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
題解:
做不出這道題的童鞋相信都卡在了這個坑裏。
all the cars running in the north-south direction wait the same amount of integral time so that no two cars bump.
去掉定語後是:all the cars wait the same time. 所有汽車等待時間相同。
這句話的意思是:所有南北方向上的汽車要等待相同的時間,以至於沒有兩輛汽車相撞。
這句話的關鍵是所有汽車等待時間相同,要麼全都是5min,要麼全都是10min,不能這輛汽車等1min、那輛汽車等2min的。
暴力枚舉即可。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
int a[maxn],b[maxn],dis[maxn];
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(dis,0,sizeof(dis));
for(int i=0;i<n;i++)
{
cin>>a[i];
dis[a[i]]=1;
}
for(int i=0;i<m;i++) cin>>b[i];
int ans;
for(int i=0;i<1001;i++)
{
int flag=0;
for(int j=0;j<m;j++)
{
if(dis[b[j]+i])
{
flag=1;
break;
}
}
if(!flag)
{
ans=i;
break;
}
}
cout<<ans<<endl;
}
return 0;
}