【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;
}
相关推荐
阿让啊5 分钟前
C语言strtol 函数使用方法
c语言·数据结构·c++·单片机·嵌入式硬件
深圳市快瞳科技有限公司6 分钟前
小场景大市场:猫狗识别算法在宠物智能设备中的应用
算法·计算机视觉·宠物
liulilittle29 分钟前
OPENPPP2 —— IP标准校验和算法深度剖析:从原理到SSE2优化实现
网络·c++·网络协议·tcp/ip·算法·ip·通信
superlls3 小时前
(算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
java·数据结构·算法
田里的水稻3 小时前
C++_队列编码实例,从末端添加对象,同时把头部的对象剔除掉,中的队列长度为设置长度NUM_OBJ
java·c++·算法
纪元A梦3 小时前
贪心算法应用:保险理赔调度问题详解
算法·贪心算法
Ripple123124 小时前
数据结构:顺序表与链表
数据结构·链表
Jayden_Ruan4 小时前
C++逆向输出一个字符串(三)
开发语言·c++·算法
liulun4 小时前
Skia如何渲染 Lottie 动画
c++·动画
一个响当当的名号5 小时前
B树,B+树,B*树(无代码)
数据结构·b树