首页 > 其他分享 >CF1045G AI robots题解

CF1045G AI robots题解

时间:2024-01-05 20:47:34浏览次数:31  
标签:node typedef AI 题解 robots pos int include define

题目链接:洛谷 或者 CF

本题考虑转化为 cdq 分治模型

对于 cdq 分治来说,只需要考虑左边对右边的影响,那我们要考虑该怎样设置第一维度的左右对象。很显而易见的是抛开 \(q\) 限制而言,我们着眼于,如何让双方互相看到的严格条件转化为只需要关注单体看见。考虑什么情况下只需要一方看到对方,对方就能看到自身。首先考虑双方互相看到的充要条件

\[1.\ pos_1-R_1 \le pos_2 \le pos_1+R_1 \]

\[2.\ pos_2-R_2 \le pos_1 \le pos_2+R_2 \]

考虑上述两个条件如何转化为一个条件,我们注意到如果有 \(R1\le R2\) 那么,很容易得知,当 \(pos_1\) 位置的机器人能看到 \(pos_2\) 位置的机器人时,它俩就互相能看到了,换句话来说,此时此刻 \(1\) 成立,即可。

简单证明就是有两个机器人,它们都处在各自圆心处在数轴上,各自有个探测半径,现在要求它们的圆心都处于对方的园内。很显而易见的有:

\[\text{圆心距} = \left |pos_1 -pos_2 \right| \le \min(R_1,R_2) \text{,证毕。} \]

那么我们就能把双方影响,变为只有一方影响另一方了,符合 cdq 模型。考虑哪个放左哪个放右?右边是受左边影响计算的。考虑计算某个 \(pos\) 的答案时,我们显然根据刚刚所说,如果它能看到其他机器人,那么其他机器人就能看到它,所以它的探测范围是用于最终查询的对象,它应该作为右边,其他机器人放在左边。通过上述,我们能知道它具备的条件为它的探测范围比其他机器人小,这样一来,它找自己探测范围内比它探测范围大的机器人时,对方一定能同时看到它。外层框架搭建完毕。

现在考虑 \(K\) 限制,很显而易见的是,题目条件所表达的意思为:

\[ \left| q_{curr}-q_{other} \right| \le K \Leftrightarrow q_{curr}-K \le q_{other} \le q_{curr}+K \]

当前查询点和其他点的 \(q\) 的绝对值之差不超过 \(K\),很显然,\(q_{curr}\) 和 \(K\) 是定值,我们如何找到 \(q_{other}\) 在左区间 \([L,mid]\) 上。根据 cdq 分治的套路,我们只需要让 \(q\) 有序就行了,这个有序依托于归并排序的排序。这样一来双指针类似莫队应用的插入和删除在一种支持单点修改,区间查询的数据结构上就行了。最后查询对应的搜索范围就行了。

cdq 分治的题型常常有这么几步,考虑外层如何转化为单个对象对单个对象有影响,从而确定左右之分,受影响和影对象。内存因为基于归并排序的结构,所以常常可以让其中一维有序,有序就具备了单调性,常常可以考虑构建双指针或者其他单调算法。最后再配上一个查询类的数据结构就行了,常常是计数类问题,所以可以考虑树状数组。

算法框架

首先我们最终使用树状数组查询探测范围,所以需要考虑离散化,我用的哈希表进行离散化,结果发现 gp_hash_table 被卡哈希了,试了下 unordered_map 或者 cc_hash_table 没问题,当然cf 上非必要还是考虑使用 map 代替哈希表功能。当然也可以用二分离散化。

其次,外层需要对探测范围排序,由于小的探测范围作为被查询对象,需要放右边,所以逆序排序就好了。最后在每次 merge 时将 \(q\) 变为有序,双指针时像类似莫队一样的 add 和 del 即可,树状数组维护 \(pos\) 用于探测范围内圆心计数。

参照代码
#include <bits/stdc++.h>

//#pragma GCC optimize("Ofast,unroll-loops")

// #define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 1e5 + 10;
set<int> t;
hash3<int, int> mp;
int n, K;
ll ans;
int mx;
int bit[N << 2];

struct Query
{
    int pos, R, l, r;
    int q;
} node[N];

inline void add(int x, const int val)
{
    for (; x <= mx; x += lowBit(x))bit[x] += val;
}

inline int query(int x)
{
    int ans = 0;
    for (; x; x -= lowBit(x))ans += bit[x];
    return ans;
}

inline int queryLR(const int L, const int R)
{
    return query(R) - query(L - 1);
}
//按探测范围降序排序
inline bool cmpLen(const Query& x, const Query& y)
{
    return x.R > y.R;
}

Query tmp[N];
//q有序数组合并
inline void merge(const int L, const int mid, const int R)
{
    int cnt = L;
    int i = L, j = mid + 1;
    while (i <= mid and j <= R)tmp[cnt++] = node[i].q <= node[j].q ? node[i++] : node[j++];
    while (i <= mid)tmp[cnt++] = node[i++];
    while (j <= R)tmp[cnt++] = node[j++];
    forn(i, L, R)node[i] = tmp[i];
}

inline void cdq(const int L, const int R)
{
    const int mid = L + R >> 1;
    if (L == R)return;
    cdq(L, mid);
    cdq(mid + 1, R);
    int l = L, r = L - 1;
    //双指针找[q-K,q+K]范围内的所有圆心,加入树状数组
    forn(curr, mid+1, R)
    {
        auto [pos,R,queryL,queryR,q] = node[curr];
        while (l <= mid and q - K > node[l].q)add(node[l++].pos, -1);
        while (r < mid and node[r + 1].q <= q + K)add(node[++r].pos, 1);
        ans += queryLR(queryL, queryR);
    }
    forn(i, l, r)add(node[i].pos, -1);
    merge(L, mid, R); //使q有序
}

inline void solve()
{
    cin >> n >> K;
    forn(i, 1, n)
    {
        auto& [pos,R,_1,_2,q] = node[i];
        cin >> pos >> R >> q;
        t.insert(pos);
        t.insert(pos + R);
        t.insert(pos - R);
    }
    for (auto v : t)mp[v] = ++mx;
    //离散化
    forn(i, 1, n)node[i].l = mp[node[i].pos - node[i].R], node[i].r = mp[node[i].pos + node[i].R], node[i].pos = mp[node[i].pos];
    sort(node + 1, node + n + 1, cmpLen);
    cdq(1, n);
    cout << ans;
}

signed int main()
{
    Spider
    //------------------------------------------------------
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
}

\[\text{最终时间复杂度显然为 } O(n\log^2{n}) \text{,第二支 log 准确一点应该为}\log{3n} \]

标签:node,typedef,AI,题解,robots,pos,int,include,define
From: https://www.cnblogs.com/Athanasy/p/17948039

相关文章

  • 什么是人工智能幻觉?为什么AI会编造答案?丨曼孚科技
    当我们欣赏由AI生成的艺术作品时,往往会被其美感和独特性所吸引,它以令人惊叹的创造力和智能对话能力,在艺术、写作、音乐等领域展现出巨大潜力,仿佛具备了人类的创作天赋。然而,近期的一些事件引发了人们对AI系统输出准确性和可信度方面的关注,即“它自信地给出了看似正确实则错误的答......
  • 蚂蚁集团持续探索生成式AI,20篇论文入选AI顶会NeurlPS
    当地时间12月10日,为期一周的全球AI顶级会议NeurlPS在美国路易斯安那州新奥尔良市举办。NeurlPS官方数据显示,本届会议共有12343篇有效论文投稿,接收率为26.1%。蚂蚁集团20篇论文被收录。据了解,蚂蚁此次入选的论文,覆盖计算机视觉、自然语言处理、图神经网络、图像处理等多个人工智能和......
  • optical strain(python)
    利用pythonopencv计算opticalstrainimportcv2importnumpyasnpclassOptFlowStrain:def__init__(self):self.TVL1=cv2.optflow.DualTVL1OpticalFlow_create()defrun(self,img0,img1):returnself.cal_opt_flow(img0,img1)def......
  • wsl2 通过桥接实现 局域网访问,docker 远程连接执行testcontainer
    为了不想在本地安装docker运行testcontainer容器测试,需要wsl2的所有端口,局域网都可以访问,折腾了3天,最后在https://zhuanlan.zhihu.com/p/659074950这篇文章的指引下才实现 首先打开控制面板点击程序 点击启用或关闭Window功能   确保开启虚拟化然后打开......
  • CES 2024前瞻:PC迈入AI时代
    备受瞩目的2024年消费电子展(CES)将于美国西部时间1月9日在拉斯维加斯开幕,即将登场的大量突破性技术进步令人期待不已。尤其是在今年,AI人工智能当仁不让地成为焦点,由AI赋能的各类产品将覆盖人们生活的方方面面。根据已有消息,包括华硕、戴尔、NVIDIA、三星、英特尔、AMD、海信、......
  • Python爬虫JS解密-baidu翻译
    请求分析参数构造流程这种提交数据得到响应的的请求,往往参数比较麻烦,所以参数的构造是得到完整请求的关键.首先我们要明确目标,爬取这个网站的目的是什么,那我们的目的就是模拟浏览器发送请求,完成翻译的功能,明确了目标之后我们再定位到相关的URL就比较容易了。现在先打开chrome的调......
  • 美国13岁少年通关原版俄罗斯方块:历史首人,此前仅AI可完成
    美国13岁少年通关原版俄罗斯方块:历史首人,此前仅AI可完成投递人 itwriter 发布于 2024-01-0417:00 评论(0) 有233人阅读 原文链接 [收藏] « »俄罗斯方块这款经典游戏想必大家都玩过,但能将其通关的人此前从未出现。近日,这一空白终于被打破。美国一名13岁......
  • AI小蜜批量写作助手:多级指令,插件,GPTs满足不同写作需求
    为什么会开发这个脚本?爆文项目的核心是矩阵怼量具体怎么做这里介绍很清楚了:AI爆文撸流量主保姆级教程3.0+脚本写作教程(解放双手)我在刚做爆文项目时候,都是手动操作,复制指令,组合指令,粘贴,AI生成内容,然后发布。整个过程流程简单,全部重复劳动。但凡没点耐心,很容易就放弃了。重复......
  • nested exception is java.lang.IllegalArgumentException异常问题解决
    项目启动报错如下:nestedexceptionisjava.lang.IllegalArgumentException:Couldnotresolveplaceholder'xxx'invalue"${xxx}"问题解决比较简单,只说我所遇到的情况,原因就是字母拼写问题仔细看还是能看到大写的K和小写的k有一些细微的区别,将nacos中的k和代码中修改一致后启......
  • 【服务器数据恢复】服务器raid5崩溃导致上层分区无法访问的数据恢复案例
    服务器数据恢复环境:北京某教育机构一台服务器中有一组由3块磁盘组建的raid5阵列,服务器安装的windowsserver操作系统。服务器故障:该服务器在运行过程中突然瘫痪,无法正常工作。北亚企安数据恢复工程师去现场对故障服务器进行了检测,发现故障服务器raid5阵列中有一块硬盘由于未知原......