Solution Set -「AGC 013~015」C~F
- 「AGC 013C」Ants on a Circle
- 「AGC 013D」Piling Up ^
- 「AGC 013E」Placing Squares ^
- 「AGC 013F」Two Faced Cards *
- 「AGC 014C」Closed Rooms
- 「AGC 014D」Black and White Tree
- 「AGC 014E」Blue and Red Tree *
- 「AGC 014F」Strange Sorting *
- 「AGC 015C」Nuske vs Phantom Thnook
- 「AGC 015D」A or...or B Problem
- 「AGC 015E」Mr. Aoki Incubator ^
- 「AGC 015F」Kenus the Ancient Greek *
Walking_Dead, known as a spy from the US in our computer room, said that he couldn't get the meaning of my solutions all the time. That's why I'm writing this solution set in English instead of Chinese.
「AGC 013C」Ants on a Circle
Tags:「C.性质/结论」「C.思维」
It's an interesting problem after all.
On the one hand, because there can't be two ants swapping their relative position at any time, the final identities of ants reading from \(x=0\) to \(x=L-1\) must be a rotation of \([1,2,\dots,n]\). On the other hand, a well-known transformation of this kind of problems is to let two ants cross each other when they meet, without turning their way. Therefore, it's easy to work out the final positions for all ants - \(\{(x_i+T)\bmod L\}_{i=1}^n\). Combining two properties above, our rest task is to find the ant \(1\)'s final position in \(\{(x_i+T)\bmod L\}_{i=1}^n\).
Difficulty comes from "circle", so let's expand it out. for an ant \(i\), let it appear at positions \(i+kn\) for all integer \(k\). By simply counting how many ants cross the position \(-0.5\), we can find the final position for the ant whose initial position is \(1\). If that position is \(x=r+kn\), it suggests the final position of ant \(1\) is \(r\). Knowing this, we can find the answer with mentioned properties quickly.
The complexity for this algorithm is \(\mathcal O(n\log n)\) due to sorting \(\{(x_i+T)\bmod L\}_{i=1}^n\).
「AGC 013D」Piling Up ^
Tags:「A.DP-计数 DP」「B.模型转化」
Confusing ... Does anyone think this problem is easier than the above one?
Initially, we separate the original operation "take-put-take" into two operations "take-put" and "take". Then, locating a state "after \(i\) operations, \(r\) red balls and \(n-i+[2\nmid i]-r\) blue balls remain in the box" at \((i,r)\) on a grid, one can see that \(2m\) operations are drawn as a broken line. And, a color sequence corresponds to a broken line that touched \(y=0\) at least once. Therefore, we can DP the number of such broken lines to solve this problem in \(\mathcal O(nm)\) time.
「AGC 013E」Placing Squares ^
「AGC 013F」Two Faced Cards *
Tags:「B.贪心」「C.思维」
Let \(n\gets n+1\). Since all we know the Hall's theorem, if given two array \(\{a_n\}\) and \(\{c_n\}\), we can check if there's a legal match as follows: maintain a sequence \([s_1,s_2,\cdots,s_n]=[0,0,\cdots,0]\) initially. Then for \(i\in[1,n]\) and \(j\in[a_i,n]\), let \(s_j\gets s_j-1\), and for \(i\in[1,n]\) and \(j\in[c_i,n]\), let \(s_j\gets s_j+1\). A match exists iff all \(s_i\ge0\) eventually.
Go back to the original problem. Greedily, we won't flip any card at the beginning, so there may be some \(s_i<0\) after performing the operation above. Remember that a query can offer an extra card \(x\), which means \(s\) is legal iff \(s_i\ge0\) for \(i<x\) and \(s_i\ge-1\) for \(i\ge x\). Regardless \(x\), we have another choice to change \(s\) - flipping a card \((a,b)\), which will increase \(s_i\) by \(1\) for \(i\in[b,a)\). Only \(n\) possible \(x\) existing, let's try to find the minimal flipping time for all \(x\in[1,n]\).
Our first task is to flip some cards to get all \(s_i\ge-1\). Maybe hearing an oracle, we suddenly come up with a greedy algorithm: scan \(s\) from \(i=n\) to \(i=1\), and if an \(s_i<-1\) is found, we continually choose a interval \([b,a)\) containing \(i\) with minimal \(b\), and flip card \((a,b)\) until \(s_i\ge-1\). We can prove the correctness through a simple swapping-discussion. In my opinion, the biggest obstacle to this solution is to choose a right scanning direction on \(s\). Counterintuitive as it is, we may reach it after having tried scanning from \(i=1\) to \(i=n\) and failed to prove a greedy algorithm. (Or, just click "Editorial" like me.)
The rest part of the task is much easier - scanning \(s\) again from \(i=1\) to \(i=n\) and making \(s_i\ge0\) with intervals \([b,a)\) containing \(i\) with maximal \(a\). When \(s_i\ge0\) for \(i<x_0\), update the answer for \(x=x_0\) by current swapping time.
Using heap to maintain data required, we can do things above in \(\mathcal O(n\log n)\) time.
「AGC 014C」Closed Rooms
Tag:「水题无 tag」
超级可爱的一道题不是吗, 对于新手的确很 educational.
不难注意到, 当我们第一次使用解锁操作后, 最优情况下, 我们不可能再被任何障碍阻挡. 也就是说, 第一次使用后, 必然一条直路直接莽到边界. 因此 BFS 枚举距离起点不超过 \(k\) 且能走到的点, 若已经到边界则答案为 \(1\), 否则设某个点到边界的距离为 \(x\), 答案就是最小的 \(1+\lceil x/k\rceil\). \(\mathcal O(nm)\).
「AGC 014D」Black and White Tree
Tag:「C.性质/结论」
推广一下样例一: 若存在一个点邻接与多余一片叶子, 显然先手必胜. 如果一个点仅邻接一片叶子, 那么先手染白这篇叶子可以抢先手, 因为后手必须立马把叶子染黑. 此时, 状况等价于这个点没有任何邻接点, 所以接着向上归纳即可. 这个归纳等价于找完美匹配, 若存在则后手胜 (经典的完美匹配下后手必胜的情况), 否则先手的确必胜, 不难证明. \(\mathcal O(n)\) 搜一遍即可.
「AGC 014E」Blue and Red Tree *
Tags:「C.性质/结论」「C.思维」
失误了属于是. 其实这个结论只要方向对了就很显然: 考虑最后一步操作, 其红边蓝边必然重合. 归纳的, 将这两条边的端点合并, 不断迭代. 若最后只剩一个结点就存在方案. 否则显然也就不存在方案. 用 std::set 之类的东西维护邻接关系, 启发式合并, 可以做到 \(\mathcal O(n\log^2n)\), 当然选用合适的平衡树种类可以做到 \(\mathcal O(n\log n)\).
「AGC 014F」Strange Sorting *
Tags:「C.性质/结论」「C.思维」
打开题解界面, 不会是真的吧.
(思路非常 unmotivated, 我无法解读, 甚至无法想象出题人的精神状态.)
这题的唯一切入点或许是一个很小的 observation: \(p_i=1\) 的位置不会影响其他元素 high 不 high, 也就是说它对我们的排序过程基本没影响.
猜猜看, 或许可以在值域上归纳? 我们只考虑值域 \([i+1,n]\) 上的元素, 假设在 \(t_{i+1}\) 步后将它们按规则排序, 此时 \(i\) 会出现在序列的某个位置. 当然, 如果恰好是第一个位置, \(t_i=t_{i+1}\), 否则 \(t_i=t_{i+1}+1\). 那现在又有一个问题, 我们似乎需要知道 \(i\) 是否恰好是对 \([i+1,n]\) 排序后的第一个元素.
仅考虑 \(t_{i+1}>0\) 的情况. 目标是判断 \(t_{i+1}\) 次操作后 \(i\) 是否恰好是 \([i,n]\) 中最前的元素. 设 \(f_{i+1}\) 表示对 \([i+1,n]\) 排序 \(t_{i+1}-1\) 次后的第一个元素. 显然 \(f_{i+1}\neq i+1\). 那么若 \(i\) 落在 \(f_{i+1}\) 和 \(i+1\) 之间, 它就会在下一次排序中被扔到开头. 神谕告诉我们, \(i\) 落在 \(f_{i+1}\) 和 \(i+1\) 之间, 等价于初始序列中, 元素 \(i,i+1,f_{i+1}\) 的出现顺序循环同构于 \([i,i+1,f_{i+1}]\). 也就是说, 操作不会改变 \([i,i+1,f_{i+1}]\) 的循环同构性.
很 AGC 的是, 我们还需要一个结论才能证明这个断言. 神谕又告诉我们, 在前 \(t_{i+1}-1\) 步中, 当且仅当 \(f_{i+1}\) 处于序列头, \(f_{i+1}\) 才 high.
考虑反证, 若 \(f_{i+1}\) 不在开头但是 high, 那么在一次操作后, \(f_{i+1}\) 会处于某个原来 high 的元素后边. 显然在这个元素 \(<f_{i+1}\), 则当且仅当这个元素 low 而 \(f_{i+1}\) 又 high 时, 它们会分开. 但分开后 \(f_{i+1}\) 又是非开头的 high, 所以它永远不能成为开头元素, 于条件矛盾.
证明结论后, 大讨论!
- \([f_{i+1},i,i+1]\), \(f_{i+1}\) 开头, 三个元素依次为 high, low, low, 操作后仍然同构.
- \([i+1,f_{i+1},i]\), \(i+1\) 开头, high, low, low, 同理.
- \([i,i+1,f_{i+1}]\), \(i\) 开头, high, high, low, 同理.
- 都不是开头元素, low, low, low, 同理.
证明完所有结论, 从 \(n-1\) 到 \(1\) 递推一边 \(t\) 和 \(f\) 就能求出答案. \(\mathcal O(n)\).
「AGC 015C」Nuske vs Phantom Thnook
Tag:「水题无 tag」
树上连通块数 = 结点数 - 导出子图边数. 滚几个二维前缀和就行了. \(\mathcal O(nm)-\mathcal O(1)\).
「AGC 015D」A or...or B Problem
Tag:「C.细节」
你甜美的耐心题, 整吐了.
去掉相同高位. 设 \(b\) 当前的最高为为 \(h\), 注意到如果或和值没有 \(h\) 这位, 上界就不存在了. 所以这提示我们分 \([a,2^h)\) 的部分和 \([2^h,b]\) 的部分讨论.
若全部选 \([a,2^h)\) 内的数, 可行或和区间显然就是 \([a,2^h)\);
若全部选 \([2^h,b]\) 内的数, 则 \(b\) 的次高 bit (若存在) 及其低 bit 可以乱选, 次高 bit 以上, \(h\) 以下只能是 \(0\). 将 \(b\) 的次高 bit 后全部置为 \(1\) 得到 \(b'\), 则这部分的或和区间为 \([2^h,b']\);
还有一种情况是在 \([a,2^h)\) 和 \([2^h,b]\) 内各选一些数. 经过前面的讨论, 我们有端怀疑或和也是一个区间. 下界显然是 \(a+2^h\), 上界显然是 \(2^{h+1}-1\), 难道就是 \([a+2^h,2^{h+1}-1]\)? 那确实就是这样, 证明不难.
用出色的位运算技巧可以做到 \(\mathcal O(1)\).
「AGC 015E」Mr. Aoki Incubator ^
Tags:「A.DP-杂项」「C.性质/结论」
考虑所有碰撞完成的情况, 所有球按 \(v\) 升序排列. 经过简单讨论, 若 \(v_i<v_j<v_k\) 且球 \(i\) 感染过球 \(k\), 那么球 \(j\) 也一定被感染. 因此, 每个球能够感染的是包含自己的一段区间. 预处理出这个区间, 我们将要计数的是: 选择区间, 覆盖 \([1,n]\) 的方案数.
哈, 前缀和优化一下 DP 就行了. 瓶颈是 \(\mathcal O(n\log n)\) 排序.
「AGC 015F」Kenus the Ancient Greek *
Tags:「C.性质/结论」「C.思维」
最大值比较容易打表观察或者构造得出: 不妨令 \(x\le y\), 则最大迭代次数 \(k\) 为最大的满足 \(F_{k-1}\le x\land F_k\le y\) 的 \(k\), 其中 \(F_k\) 表示 Fibonacci 数列的第 \(k\) 项 (\(F_0=F_1=1\)).
难点在于求方案数. 令 \(f(x,y)\) 表示 \((x,y)\) 需要的迭代次数, \(g(i,j)\) 为 \(f\) 的前缀 \(\max\). 现在, 我们定义 good pair 表示满足 \(f(x,y)=g(x,y)\) 的数对 \((x,y)\). 我们的工作是数出所有满足 \(f(x,y)=k\) 的 good pair 的数量.
但是 ... 这个转化并没有什么用, good pair 的数量仍然很多. 尝试让 good pair 迭代一次? 我们再定义所有能由 \((k+1)-\)good pair 迭代一次得到的数对为 \(k-\)excellent pair, 目测发现, \(k-\)excellent pair 的数量也许能够接受.
再观察一下 excellent pair 的位置? 事实上, 数对 \((x,y)~(x<y)\) 是 \(k-\)excellent pair, 当且仅当 \(f(x,y)=k\) 且 \(x,y\le F_{k+2}\). 证明方面, 考虑取任意一个 \((k+1)-\)good pair \((x,px+y)\), 其迭代一次后将变为 \((y,x)\). 如果 \((y,x)\) 不是 excellent pair, 那么应当有 \(x>F_{k+2}\). 我们已知 \(f(F_{k+1},F_{k+2})=k+1\), 而现在有 \(x>F_{k+2}\), 同时 \(f(y,x)=k\), 所以 \(y\ge F_k\), 因此 \(F_{k+2}=F_{k+1}+F_k<px+y\), 这与 \((x,px+y)\) 是 good pair 相矛盾! (注: 官方题解证明这部分貌似写锅了.)
显然, 我们可以通过 \(k-\)excellent pair 倒推出 \((k+1)-\)excellent pair, 此后, 一个 excellent pair \((x,y)\) 代表着 good pair \((x,y+px)\), 在回答询问时暴力枚举这样的 excellent pair 算贡献就行了. 复杂度 \(\mathcal O(\log^2 V)-\mathcal O(\log V)\).
Solution Set -「AGC 013~015」C~F的更多相关文章
- Note -「圆方树」学习笔记
目录 圆方树的定义 圆方树的构造 实现 细节 圆方树的运用 「BZOJ 3331」压力 「洛谷 P4320」道路相遇 「APIO 2018」「洛谷 P4630」铁人两项 「CF 487E」Touris ...
- Diary / Solution Set -「WC 2022」线上冬眠做噩梦
大概只有比较有意思又不过分超出能力范围的题叭. 可是兔子的"能力范围" \(=\varnothing\) qwq. 「CF 1267G」Game Relics 任意一个 ...
- Note/Solution -「洛谷 P5158」「模板」多项式快速插值
\(\mathcal{Description}\) Link. 给定 \(n\) 个点 \((x_i,y_i)\),求一个不超过 \(n-1\) 次的多项式 \(f(x)\),使得 \(f(x ...
- Solution -「洛谷 P6021」洪水
\(\mathcal{Description}\) Link. 给定一棵 \(n\) 个点的带点权树,删除 \(u\) 点的代价是该点点权 \(a_u\).\(m\) 次操作: 修改单点点权. ...
- Solution -「APIO 2016」「洛谷 P3643」划艇
\(\mathcal{Description}\) Link & 双倍经验. 给定 \(n\) 个区间 \([a_i,b_i)\)(注意原题是闭区间,这里只为方便后文描述),求 \(\ ...
- LOJ6003 - 「网络流 24 题」魔术球
原题链接 Description 假设有根柱子,现要按下述规则在这根柱子中依次放入编号为的球. 每次只能在某根柱子的最上面放球. 在同一根柱子中,任何2个相邻球的编号之和为完全平方数. 试设计一个算法 ...
- LOJ6002 - 「网络流 24 题」最小路径覆盖
原题链接 Description 求一个DAG的最小路径覆盖,并输出一种方案. Solution 模板题啦~ Code //「网络流 24 题」最小路径覆盖 #include <cstdio&g ...
- LOJ6001 - 「网络流 24 题」太空飞行计划
原题链接 Description 有个实验和个仪器,做实验有报酬买仪器有花费.每个实验都需要一些仪器,求最大净收益(实验报酬仪器花费),并输出一组方案. Solution 实验向所需仪器连边,实验的点 ...
- 「NowCoder Contest 295」H. Playing games
还是见的题太少了 「NowCoder Contest 295」H. Playing games 题意:选出尽量多的数使得异或和为$ 0$ $ Solution:$ 问题等价于选出尽量少的数使得异或和为 ...
- Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流)
Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流) Description 问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同 ...
随机推荐
- 我开源了一个短视频应用(Go+React)|DouTok2.0 项目介绍
前言 大家好,这里是白泽,拖更了一段时间,抱歉.在 DouTok2.0 可以初步允许大家接入开发之后,这篇文章才得以出炉. DouTok:一个开源的 web 端的短视频应用,采用微服务架构,包含前后端 ...
- DDCA —— 缓存一致性
1. 多处理器内存组织结构 1.1 SMP/集中式共享内存 集中式共享内存多处理器(Centralized shared-memory multiprocessor)或对称共享内存多处理器(Symme ...
- Vulhub Apache Httpd漏洞复现
目录 前言 多后缀解析漏洞 换行解析漏洞(CVE-2017-15715) 2.4.49 路径穿越漏洞(CVE-2021-41773) 2.4.50 路径穿越漏洞(CVE-2021-42013) SSR ...
- 个人wiki
1:记录自己的知识体系 2:轻量级wiki系统(排除XWiki) 3:开源 4:支持通用wiki语法(排除dokuwiki) 5:有好的编辑器(排除MediaWiki) 6:最好是java,或者php ...
- golang之JSON处理
在强类型语言中,JSON 通常很难处理 -- JSON 类型有字符串.数字.字典和数组.如果你使用的语言是 javascript.python.ruby 或 PHP,那么 JSON 有一个很大的好处就 ...
- Phpstorm之快捷键
常用快捷键 1.ctrl+alt+s 快速打开setting系统设置 2.CTRL+/ 单行注释/取消注释 CTRL+SHIFT+/ 块状注释/取消块状注释 3.方法体上面打入'/**' 再按回车键 ...
- mysql 自定义函数写法
1.业务场景 有时候我们希望通过sql语句解决一些复杂的问题,比如根据一个ID 查询组织的路径.这个时候我们可以使用函数来实现. 2.函数编写 CREATE FUNCTION getGroupById ...
- HTML5 网络监听,全屏
1.网络状态监听 online事件:网络重新连通时触发 offline事件:网络断开时触发 <script> // 监听网络连接 window.addEventListener(" ...
- Java基础面试:关键字与注释
Java 中的关键字 什么是关键字 Java 关键字是 Java 语言中预先定义好的.具有特殊含义的标识符.这些标识符在程序中有固定的用途,不能用作变量名.方法名或类名.Java 中共有 53 个特殊 ...
- 搭建一个文件存储服务器minio,实现文件存储
搭建一个文件存储服务器minio,实现文件存储 Minio是一个开源的.自托管的对象存储服务器,它提供了类似于云存储服务的功能.你可以使用Minio搭建自己的私有云存储解决方案,或者作为公共存储服务的 ...