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 to must be a rotation of . 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 - . Combining two properties above, our rest task is to find the ant 's final position in .
Difficulty comes from "circle", so let's expand it out. for an ant , let it appear at positions for all integer . By simply counting how many ants cross the position , we can find the final position for the ant whose initial position is . If that position is , it suggests the final position of ant is . Knowing this, we can find the answer with mentioned properties quickly.
The complexity for this algorithm is due to sorting .
「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 operations, red balls and blue balls remain in the box" at on a grid, one can see that operations are drawn as a broken line. And, a color sequence corresponds to a broken line that touched at least once. Therefore, we can DP the number of such broken lines to solve this problem in time.
「AGC 013E」Placing Squares ^
「AGC 013F」Two Faced Cards *
Tags:「B.贪心」「C.思维」
Let . Since all we know the Hall's theorem, if given two array and , we can check if there's a legal match as follows: maintain a sequence initially. Then for and , let , and for and , let . A match exists iff all eventually.
Go back to the original problem. Greedily, we won't flip any card at the beginning, so there may be some after performing the operation above. Remember that a query can offer an extra card , which means is legal iff for and for . Regardless , we have another choice to change - flipping a card , which will increase by for . Only possible existing, let's try to find the minimal flipping time for all .
Our first task is to flip some cards to get all . Maybe hearing an oracle, we suddenly come up with a greedy algorithm: scan from to , and if an is found, we continually choose a interval containing with minimal , and flip card until . 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 . Counterintuitive as it is, we may reach it after having tried scanning from to and failed to prove a greedy algorithm. (Or, just click "Editorial" like me.)
The rest part of the task is much easier - scanning again from to and making with intervals containing with maximal . When for , update the answer for by current swapping time.
Using heap to maintain data required, we can do things above in time.
「AGC 014C」Closed Rooms
Tag:「水题无 tag」
超级可爱的一道题不是吗, 对于新手的确很 educational.
不难注意到, 当我们第一次使用解锁操作后, 最优情况下, 我们不可能再被任何障碍阻挡. 也就是说, 第一次使用后, 必然一条直路直接莽到边界. 因此 BFS 枚举距离起点不超过 且能走到的点, 若已经到边界则答案为 , 否则设某个点到边界的距离为 , 答案就是最小的 . .
「AGC 014D」Black and White Tree
Tag:「C.性质/结论」
推广一下样例一: 若存在一个点邻接与多余一片叶子, 显然先手必胜. 如果一个点仅邻接一片叶子, 那么先手染白这篇叶子可以抢先手, 因为后手必须立马把叶子染黑. 此时, 状况等价于这个点没有任何邻接点, 所以接着向上归纳即可. 这个归纳等价于找完美匹配, 若存在则后手胜 (经典的完美匹配下后手必胜的情况), 否则先手的确必胜, 不难证明. 搜一遍即可.
「AGC 014E」Blue and Red Tree *
Tags:「C.性质/结论」「C.思维」
失误了属于是. 其实这个结论只要方向对了就很显然: 考虑最后一步操作, 其红边蓝边必然重合. 归纳的, 将这两条边的端点合并, 不断迭代. 若最后只剩一个结点就存在方案. 否则显然也就不存在方案. 用 std::set
之类的东西维护邻接关系, 启发式合并, 可以做到 , 当然选用合适的平衡树种类可以做到 .
「AGC 014F」Strange Sorting *
Tags:「C.性质/结论」「C.思维」
打开题解界面, 不会是真的吧.
(思路非常 unmotivated, 我无法解读, 甚至无法想象出题人的精神状态.)
这题的唯一切入点或许是一个很小的 observation: 的位置不会影响其他元素 high 不 high, 也就是说它对我们的排序过程基本没影响.
猜猜看, 或许可以在值域上归纳? 我们只考虑值域 上的元素, 假设在 步后将它们按规则排序, 此时 会出现在序列的某个位置. 当然, 如果恰好是第一个位置, , 否则 . 那现在又有一个问题, 我们似乎需要知道 是否恰好是对 排序后的第一个元素.
仅考虑 的情况. 目标是判断 次操作后 是否恰好是 中最前的元素. 设 表示对 排序 次后的第一个元素. 显然 . 那么若 落在 和 之间, 它就会在下一次排序中被扔到开头. 神谕告诉我们, 落在 和 之间, 等价于初始序列中, 元素 的出现顺序循环同构于 . 也就是说, 操作不会改变 的循环同构性.
很 AGC 的是, 我们还需要一个结论才能证明这个断言. 神谕又告诉我们, 在前 步中, 当且仅当 处于序列头, 才 high.
考虑反证, 若 不在开头但是 high, 那么在一次操作后, 会处于某个原来 high 的元素后边. 显然在这个元素 , 则当且仅当这个元素 low 而 又 high 时, 它们会分开. 但分开后 又是非开头的 high, 所以它永远不能成为开头元素, 于条件矛盾.
证明结论后, 大讨论!
- , 开头, 三个元素依次为 high, low, low, 操作后仍然同构.
- , 开头, high, low, low, 同理.
- , 开头, high, high, low, 同理.
- 都不是开头元素, low, low, low, 同理.
证明完所有结论, 从 到 递推一边 和 就能求出答案. .
「AGC 015C」Nuske vs Phantom Thnook
Tag:「水题无 tag」
树上连通块数 = 结点数 - 导出子图边数. 滚几个二维前缀和就行了. .
「AGC 015D」A or...or B Problem
Tag:「C.细节」
你甜美的耐心题, 整吐了.
去掉相同高位. 设 当前的最高为为 , 注意到如果或和值没有 这位, 上界就不存在了. 所以这提示我们分 的部分和 的部分讨论.
若全部选 内的数, 可行或和区间显然就是 ;
若全部选 内的数, 则 的次高 bit (若存在) 及其低 bit 可以乱选, 次高 bit 以上, 以下只能是 . 将 的次高 bit 后全部置为 得到 , 则这部分的或和区间为 ;
还有一种情况是在 和 内各选一些数. 经过前面的讨论, 我们有端怀疑或和也是一个区间. 下界显然是 , 上界显然是 , 难道就是 ? 那确实就是这样, 证明不难.
用出色的位运算技巧可以做到 .
「AGC 015E」Mr. Aoki Incubator ^
Tags:「A.DP-杂项」「C.性质/结论」
考虑所有碰撞完成的情况, 所有球按 升序排列. 经过简单讨论, 若 且球 感染过球 , 那么球 也一定被感染. 因此, 每个球能够感染的是包含自己的一段区间. 预处理出这个区间, 我们将要计数的是: 选择区间, 覆盖 的方案数.
哈, 前缀和优化一下 DP 就行了. 瓶颈是 排序.
「AGC 015F」Kenus the Ancient Greek *
Tags:「C.性质/结论」「C.思维」
最大值比较容易打表观察或者构造得出: 不妨令 , 则最大迭代次数 为最大的满足 的 , 其中 表示 Fibonacci 数列的第 项 ().
难点在于求方案数. 令 表示 需要的迭代次数, 为 的前缀 . 现在, 我们定义 good pair 表示满足 的数对 . 我们的工作是数出所有满足 的 good pair 的数量.
但是 ... 这个转化并没有什么用, good pair 的数量仍然很多. 尝试让 good pair 迭代一次? 我们再定义所有能由 good pair 迭代一次得到的数对为 excellent pair, 目测发现, excellent pair 的数量也许能够接受.
再观察一下 excellent pair 的位置? 事实上, 数对 是 excellent pair, 当且仅当 且 . 证明方面, 考虑取任意一个 good pair , 其迭代一次后将变为 . 如果 不是 excellent pair, 那么应当有 . 我们已知 , 而现在有 , 同时 , 所以 , 因此 , 这与 是 good pair 相矛盾! (注: 官方题解证明这部分貌似写锅了.)
显然, 我们可以通过 excellent pair 倒推出 excellent pair, 此后, 一个 excellent pair 代表着 good pair , 在回答询问时暴力枚举这样的 excellent pair 算贡献就行了. 复杂度 .
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现