链接:P5804 [SEERC 2019] Absolute Game - 洛谷
题目:
思路:
比较经典
不难想到,由于二者都是最优的,那我们只需要考虑最后的结果就行了
由于 A 其实是可以任选的,而 A 选完后 B 就会受限制的选择,所以我们可以暴力枚举 A 最后选什么,然后就能得出 B 选什么了
具体的,对于 A 选的任意数 x,B 一定会选择一个最接近 x 的数 y,使得 abs(x - y) 最小
因此我们可以暴力枚举 x,然后 二分 找到 y,最后从所有的 abs(x - y) 中选一个最大值即可
为什么是选最大值呢?因为我们上面只约束了 B,而没考虑 A 的约束,因此我们要时刻记录最大值来充当 A 的约束
代码:
cpp
#include <iostream>
#include <algorithm>
#include<cstring>
#include <iomanip>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <array>
#include <tuple>
using namespace std;
#define int long long
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
void solve()
{
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
for (int i = 0; i < n; i++)
{
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int ans = 0;
for (int i = 0; i < n; i++)
{
auto it = lower_bound(b.begin(), b.end(), a[i]);
int tempans = 1e18;
if (it != b.end())
{
tempans = min(tempans, *it - a[i]);
}
if (it != b.begin())
{
it--;
tempans = min(tempans, a[i] - *it);
}
ans = max(ans, tempans);
}
cout << ans << endl;
}
signed main()
{
//cin.tie(0)->sync_with_stdio(false);
int t = 1;
//cin >> t;
while (t--)
{
solve();
}
return 0;
}