中美代表

牛客網題目鏈接

可以優化

先暴力求解了,由於數據量比較小。

#include <cstdio>
#include <iostream>
#include <string>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1005;
int arr[N], brr[N];
int main(){
	int n, m;
	while(cin>>n>>m){
		int res = 0x3f3f3f3f;
		for(int i = 0; i < n; i++){
			cin>>arr[i];
		}
		
		for(int i = 0; i < m; i++){
			cin>>brr[i];
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				res = min(res, abs(arr[i] - brr[j]));
			}
		}
		cout<<res<<endl;
	}
	return 0;
}
發佈了1154 篇原創文章 · 獲贊 46 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章