【Luogu】每日一题——Day4. P5804 [SEERC 2019] Absolute Game (思维 博弈论)

链接: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;
}
相关推荐
LYFlied5 小时前
【每日算法】LeetCode 153. 寻找旋转排序数组中的最小值
数据结构·算法·leetcode·面试·职场和发展
唐装鼠5 小时前
rust自动调用Deref(deepseek)
开发语言·算法·rust
Lucas555555555 小时前
现代C++四十不惑:AI时代系统软件的基石与新征程
开发语言·c++·人工智能
ytttr8736 小时前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
_MyFavorite_6 小时前
cl报错+安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
charlie1145141916 小时前
现代嵌入式C++教程:C++98——从C向C++的演化(2)
c语言·开发语言·c++·学习·嵌入式·教程·现代c++
zmzb01036 小时前
C++课后习题训练记录Day55
开发语言·c++
李白同学6 小时前
C++:继承
开发语言·c++
k***92166 小时前
【C++】STL详解(九)—priority_queue的使用与模拟实现
开发语言·c++
Hard but lovely7 小时前
C++11: 自定义异常&&标准异常体系&&回顾c异常处理方式
开发语言·c++