【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;
}
相关推荐
承渊政道4 分钟前
动态内存管理
c语言·c++·经验分享·c#·visual studio
best_virtuoso5 分钟前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
孤独得猿21 分钟前
聊天室项目开发——etcd的安装和使用
linux·服务器·c++·etcd
伏小白白白28 分钟前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习
new coder29 分钟前
[c++语法学习]Day10:c++引用
开发语言·c++·学习
无敌最俊朗@1 小时前
数组-力扣hot56-合并区间
数据结构·算法·leetcode
囚生CY1 小时前
【速写】优化的深度与广度(Adam & Moun)
人工智能·python·算法
哼?~2 小时前
C++11标准 上 (万字解析)
开发语言·c++
码农多耕地呗2 小时前
力扣94.二叉树的中序遍历(递归and迭代法)(java)
数据结构·算法·leetcode
给大佬递杯卡布奇诺2 小时前
FFmpeg 基本API avformat_alloc_context 函数内部调用流程分析
c++·ffmpeg·音视频