#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1e18
vector<int> a;
vector<int> b;
int memo[5009][5009];
int dp(int l, int r){
if(l == -1 || r == -1){
if(l == -1) return 0;
else return INF/2;
}
if(memo[l][r] != -1) return memo[l][r];
return memo[l][r] = min(dp(l,r-1),max(dp(l-1,r-1),abs(a[l]-b[r])));
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
a.resize(n);
b.resize(m);
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(n <= 5000 && m <= 5000){
for(int i = 0; i < 5009; i++){
for(int j = 0; j < 5009; j++){
memo[i][j] = -1;
}
}
vector<int> t(a);
if(n > m){
swap(a,b);
}
cout << dp(min(n,m)-1, max(n,m)-1) << endl;
}
else{
int maxi = -INF;
for(int i = 0; i < m; i++){
maxi = max(maxi, abs(a[i]-b[i]));
}
cout << maxi << endl;
}
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2040 KB |
Output is correct |
2 |
Correct |
43 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
1920 KB |
Output is correct |
2 |
Correct |
48 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
196880 KB |
Output is correct |
2 |
Correct |
314 ms |
197144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
197044 KB |
Output is correct |
2 |
Correct |
320 ms |
197240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
197096 KB |
Output is correct |
2 |
Correct |
267 ms |
197220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
197212 KB |
Output is correct |
2 |
Correct |
280 ms |
197336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
197108 KB |
Output is correct |
2 |
Correct |
277 ms |
197232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
51 ms |
3192 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
48 ms |
3316 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
40 ms |
2968 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |