// NK
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)
/*
3 5 5 5 7 8 8 9
3 5 5 7 8
brute force?
dp?
it is ok that it is sorted, so only matches are important
f(int pos, int pos2){
make it match -> ans1 = max( abs(pos - pos2), then f(pos-1, pos2-1) )
don't match -> ans2 = f(pos-1, pos2)
return min(ans1, ans2)
}
*/
vi a,b;
const int MAXS = 5005;
int dp[MAXS][MAXS];
int f(int posa, int posb){
if (posa == -1) return 0;
if (dp[posa][posb] != -1) return dp[posa][posb];
int ans1, ans2;
ans1 = ans2 = 2e9;
if (posa < posb){
ans1 = max( abs(a[posa] - b[posb]), f(posa-1, posb-1) );
ans2 = f(posa, posb-1);
}else{
ans1 = max( abs(a[posa] - b[posb]), f(posa-1, posb-1) );
}
return dp[posa][posb] = min(ans1, ans2);
}
void solve(){
int n,m; cin>>n>>m;
for (int i=0; i<n; ++i){
int x; cin>>x;
a.pb(x);
}
for (int i=0; i<m; ++i){
int x; cin>>x;
b.pb(x);
}
sort(all(a)), sort(all(b));
if (a.size() > b.size()) swap(a,b);
if (a.size() == b.size()){
int curr = 0;
for (int j=0; j<a.size(); ++j) curr = max(curr, abs(a[j] - b[j]));
cout<<curr<<'\n';
return;
}
memset(dp, -1, sizeof dp);
cout<<f(a.size()-1, b.size()-1)<<'\n';
}
int main(){
Bolivia_is_nice;
int t = 1; //cin>>t;
while(t--)
solve();
return 0;
}
/*
~/.emacs
*/
Compilation message
cipele.cpp: In function 'void solve()':
cipele.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int j=0; j<a.size(); ++j) curr = max(curr, abs(a[j] - b[j]));
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1532 KB |
Output is correct |
2 |
Correct |
41 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
1532 KB |
Output is correct |
2 |
Correct |
40 ms |
1532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
98552 KB |
Output is correct |
2 |
Correct |
60 ms |
98680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
98680 KB |
Output is correct |
2 |
Correct |
61 ms |
98808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
98680 KB |
Output is correct |
2 |
Correct |
60 ms |
98832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
98732 KB |
Output is correct |
2 |
Correct |
60 ms |
98808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
98680 KB |
Output is correct |
2 |
Correct |
60 ms |
98836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
228 ms |
200884 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
230 ms |
200812 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
233 ms |
200692 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |