【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;
}
相关推荐
今天背单词了吗9807 分钟前
算法学习笔记:25.回溯算法之迷宫寻路——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·学习·考研·算法·回溯算法
R-G-B24 分钟前
【46】MFC入门到精通——MFC显示实时时间,获取系统当前时间GetCurrentTime()、获取本地时间GetLocalTime()
c++·mfc·mfc显示实时时间·mfc获取系统当前时间·getcurrenttime·getlocaltime
鹅毛在路上了42 分钟前
CMake-gdb调试,解决LLVM ERROR: out of memory
c++·gdb·llvm
tomly202044 分钟前
【小米训练营】C++方向 实践项目 Android Player
android·开发语言·c++·jni
大专生学编程1 小时前
QT简介和QT环境搭建
c++·qt
feiyangqingyun1 小时前
Qt/C++开发监控GB28181系统/视频点播没有ssrc问题的处理/兼容各种设备和应用场景需求
c++·qt·gb28181
乌萨奇也要立志学C++2 小时前
【C++详解】STL-stack、queue的模拟实现,容器适配器,deque双端队列介绍
c++
刚入门的大一新生2 小时前
C++进阶-AVL树(平衡二叉查找树)(难度较高)
开发语言·c++
吃个糖糖2 小时前
4.PCL点云的数据结构
数据结构·算法
MyOaSis2 小时前
拆解并实现KMP算法
算法