【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;
}
相关推荐
啊阿狸不会拉杆22 分钟前
《算法导论》第 12 章 - 二叉搜索树
数据结构·c++·算法·排序算法
范特西_32 分钟前
不同的子序列-二维动态规划
算法·动态规划
aluluka1 小时前
Emacs 折腾日记(二十九)—— 打造C++ IDE
c++·ide·emacs
花开富贵ii1 小时前
代码随想录算法训练营第三十八天、三十九天|动态规划part11、12
java·数据结构·算法·leetcode·动态规划
惊骇世俗王某人3 小时前
MySQL数据库索引及底层数据结构
数据结构·数据库·mysql
HW-BASE6 小时前
《C语言》指针练习题--1
c语言·开发语言·单片机·算法·c
泽虞6 小时前
数据结构与算法
c语言·数据结构·算法
max5006007 小时前
深度学习的视觉惯性里程计(VIO)算法优化实践
人工智能·深度学习·算法
岁忧7 小时前
(nice!!!)(LeetCode 每日一题) 3363. 最多可收集的水果数目 (深度优先搜索dfs)
java·c++·算法·leetcode·go·深度优先
shenghaide_jiahu9 小时前
数学建模——粒子群算法
算法·数学建模