Live2D

Solution Set -「AGC 013~015」C~F

  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=L1 must be a rotation of [1,2,,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 - {(xi+T)modL}i=1n. Combining two properties above, our rest task is to find the ant 1's final position in {(xi+T)modL}i=1n.

  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 O(nlogn) due to sorting {(xi+T)modL}i=1n.

「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 ni+[2i]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 O(nm) time.

「AGC 013E」Placing Squares ^

  Solution.

「AGC 013F」Two Faced Cards *

  Tags:「B.贪心」「C.思维」


  Let nn+1. Since all we know the Hall's theorem, if given two array {an} and {cn}, we can check if there's a legal match as follows: maintain a sequence [s1,s2,,sn]=[0,0,,0] initially. Then for i[1,n] and j[ai,n], let sjsj1, and for i[1,n] and j[ci,n], let sjsj+1. A match exists iff all si0 eventually.

  Go back to the original problem. Greedily, we won't flip any card at the beginning, so there may be some si<0 after performing the operation above. Remember that a query can offer an extra card x, which means s is legal iff si0 for i<x and si1 for ix. Regardless x, we have another choice to change s - flipping a card (a,b), which will increase si by 1 for i[b,a). Only n possible x existing, let's try to find the minimal flipping time for all x[1,n].

  Our first task is to flip some cards to get all si1. Maybe hearing an oracle, we suddenly come up with a greedy algorithm: scan s from i=n to i=1, and if an si<1 is found, we continually choose a interval [b,a) containing i with minimal b, and flip card (a,b) until si1. 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 si0 with intervals [b,a) containing i with maximal a. When si0 for i<x0, update the answer for x=x0 by current swapping time.

  Using heap to maintain data required, we can do things above in O(nlogn) time.

「AGC 014C」Closed Rooms

  Tag:「水题无 tag」


  超级可爱的一道题不是吗, 对于新手的确很 educational.

  不难注意到, 当我们第一次使用解锁操作后, 最优情况下, 我们不可能再被任何障碍阻挡. 也就是说, 第一次使用后, 必然一条直路直接莽到边界. 因此 BFS 枚举距离起点不超过 k 且能走到的点, 若已经到边界则答案为 1, 否则设某个点到边界的距离为 x, 答案就是最小的 1+x/k. O(nm).

「AGC 014D」Black and White Tree

  Tag:「C.性质/结论」


  推广一下样例一: 若存在一个点邻接与多余一片叶子, 显然先手必胜. 如果一个点仅邻接一片叶子, 那么先手染白这篇叶子可以抢先手, 因为后手必须立马把叶子染黑. 此时, 状况等价于这个点没有任何邻接点, 所以接着向上归纳即可. 这个归纳等价于找完美匹配, 若存在则后手胜 (经典的完美匹配下后手必胜的情况), 否则先手的确必胜, 不难证明. O(n) 搜一遍即可.

「AGC 014E」Blue and Red Tree *

  Tags:「C.性质/结论」「C.思维」


  失误了属于是. 其实这个结论只要方向对了就很显然: 考虑最后一步操作, 其红边蓝边必然重合. 归纳的, 将这两条边的端点合并, 不断迭代. 若最后只剩一个结点就存在方案. 否则显然也就不存在方案. 用 std::set 之类的东西维护邻接关系, 启发式合并, 可以做到 O(nlog2n), 当然选用合适的平衡树种类可以做到 O(nlogn).

「AGC 014F」Strange Sorting *

  Tags:「C.性质/结论」「C.思维」


打开题解界面, 不会是真的吧.

  (思路非常 unmotivated, 我无法解读, 甚至无法想象出题人的精神状态.)

  这题的唯一切入点或许是一个很小的 observation: pi=1 的位置不会影响其他元素 high 不 high, 也就是说它对我们的排序过程基本没影响.

  猜猜看, 或许可以在值域上归纳? 我们只考虑值域 [i+1,n] 上的元素, 假设在 ti+1 步后将它们按规则排序, 此时 i 会出现在序列的某个位置. 当然, 如果恰好是第一个位置, ti=ti+1, 否则 ti=ti+1+1. 那现在又有一个问题, 我们似乎需要知道 i 是否恰好是对 [i+1,n] 排序后的第一个元素.

  仅考虑 ti+1>0 的情况. 目标是判断 ti+1 次操作后 i 是否恰好是 [i,n] 中最前的元素. 设 fi+1 表示对 [i+1,n] 排序 ti+11 次后的第一个元素. 显然 fi+1i+1. 那么若 i 落在 fi+1i+1 之间, 它就会在下一次排序中被扔到开头. 神谕告诉我们, i 落在 fi+1i+1 之间, 等价于初始序列中, 元素 i,i+1,fi+1 的出现顺序循环同构于 [i,i+1,fi+1]. 也就是说, 操作不会改变 [i,i+1,fi+1] 的循环同构性.

  很 AGC 的是, 我们还需要一个结论才能证明这个断言. 神谕又告诉我们, 在前 ti+11 步中, 当且仅当 fi+1 处于序列头, fi+1 才 high.

  考虑反证, 若 fi+1 不在开头但是 high, 那么在一次操作后, fi+1 会处于某个原来 high 的元素后边. 显然在这个元素 <fi+1, 则当且仅当这个元素 low 而 fi+1 又 high 时, 它们会分开. 但分开后 fi+1 又是非开头的 high, 所以它永远不能成为开头元素, 于条件矛盾.

  证明结论后, 大讨论!

  • [fi+1,i,i+1], fi+1 开头, 三个元素依次为 high, low, low, 操作后仍然同构.
  • [i+1,fi+1,i], i+1 开头, high, low, low, 同理.
  • [i,i+1,fi+1], i 开头, high, high, low, 同理.
  • 都不是开头元素, low, low, low, 同理.

  证明完所有结论, 从 n11 递推一边 tf 就能求出答案. O(n).

「AGC 015C」Nuske vs Phantom Thnook

  Tag:「水题无 tag」


  树上连通块数 = 结点数 - 导出子图边数. 滚几个二维前缀和就行了. O(nm)O(1).

「AGC 015D」A or...or B Problem

  Tag:「C.细节」


  你甜美的耐心题, 整吐了.

  去掉相同高位. 设 b 当前的最高为为 h, 注意到如果或和值没有 h 这位, 上界就不存在了. 所以这提示我们分 [a,2h) 的部分和 [2h,b] 的部分讨论.

  若全部选 [a,2h) 内的数, 可行或和区间显然就是 [a,2h);

  若全部选 [2h,b] 内的数, 则 b 的次高 bit (若存在) 及其低 bit 可以乱选, 次高 bit 以上, h 以下只能是 0. 将 b 的次高 bit 后全部置为 1 得到 b, 则这部分的或和区间为 [2h,b];

  还有一种情况是在 [a,2h)[2h,b] 内各选一些数. 经过前面的讨论, 我们有端怀疑或和也是一个区间. 下界显然是 a+2h, 上界显然是 2h+11, 难道就是 [a+2h,2h+11]? 那确实就是这样, 证明不难.

  用出色的位运算技巧可以做到 O(1).

「AGC 015E」Mr. Aoki Incubator ^

  Tags:「A.DP-杂项」「C.性质/结论」


  考虑所有碰撞完成的情况, 所有球按 v 升序排列. 经过简单讨论, 若 vi<vj<vk 且球 i 感染过球 k, 那么球 j 也一定被感染. 因此, 每个球能够感染的是包含自己的一段区间. 预处理出这个区间, 我们将要计数的是: 选择区间, 覆盖 [1,n] 的方案数.

  哈, 前缀和优化一下 DP 就行了. 瓶颈是 O(nlogn) 排序.

「AGC 015F」Kenus the Ancient Greek *

  Tags:「C.性质/结论」「C.思维」


  最大值比较容易打表观察或者构造得出: 不妨令 xy, 则最大迭代次数 k 为最大的满足 Fk1xFkyk, 其中 Fk 表示 Fibonacci 数列的第 k 项 (F0=F1=1).

  难点在于求方案数. 令 f(x,y) 表示 (x,y) 需要的迭代次数, g(i,j)f 的前缀 max. 现在, 我们定义 good pair 表示满足 f(x,y)=g(x,y) 的数对 (x,y). 我们的工作是数出所有满足 f(x,y)=kgood pair 的数量.

  但是 ... 这个转化并没有什么用, good pair 的数量仍然很多. 尝试让 good pair 迭代一次? 我们再定义所有能由 (k+1)good pair 迭代一次得到的数对为 kexcellent pair, 目测发现, kexcellent pair 的数量也许能够接受.

  再观察一下 excellent pair 的位置? 事实上, 数对 (x,y) (x<y)kexcellent pair, 当且仅当 f(x,y)=kx,yFk+2. 证明方面, 考虑取任意一个 (k+1)good pair (x,px+y), 其迭代一次后将变为 (y,x). 如果 (y,x) 不是 excellent pair, 那么应当有 x>Fk+2. 我们已知 f(Fk+1,Fk+2)=k+1, 而现在有 x>Fk+2, 同时 f(y,x)=k, 所以 yFk, 因此 Fk+2=Fk+1+Fk<px+y, 这与 (x,px+y)good pair 相矛盾! (注: 官方题解证明这部分貌似写锅了.)

  显然, 我们可以通过 kexcellent pair 倒推出 (k+1)excellent pair, 此后, 一个 excellent pair (x,y) 代表着 good pair (x,y+px), 在回答询问时暴力枚举这样的 excellent pair 算贡献就行了. 复杂度 O(log2V)O(logV).

posted @   Rainybunny  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示