JDK-8299339 : HashMap merge and compute methods can cause odd resizing pathologies
贴一下去年发现的 Bug,嘿嘿。
导致 Bug 的示例代码如下:
1 | public class Main { |
Java Bug DataBase 链接,里面比较详细的讨论了发生的问题,由于当时急着发出去,我的评论有点乱,而且是中文翻译为英文的,有点拉。
JDK-8299339 : HashMap merge and compute methods can cause odd resizing pathologies
贴一下去年发现的 Bug,嘿嘿。
导致 Bug 的示例代码如下:
1 | public class Main { |
Java Bug DataBase 链接,里面比较详细的讨论了发生的问题,由于当时急着发出去,我的评论有点乱,而且是中文翻译为英文的,有点拉。
Codeforces Round 902 (Div. 2, based on COMPFEST 15 - Final Round)
所有效率之和等于零。
1 | public static void solve() { |
贪心选择最小花费的通知,注意维护已经被通知的下标,根据该下标判断是否需要加 \(p\),并且如果某个人通知其他人的成本大于 \(p\),则他不会通知其他人。发现大佬的解法好简单,取一个最小值,维护一个剩余人数即可。
1 | public static void solve() { |
分类讨论,注意被 \(n\) 整除的数。
1 | public static void solve() { |
很容易的想到计算选择每个索引位置,能够得到的最大分数,时间复杂度为 \(O(n\log{n})\)。然后计算每个位置有多少种方案,比赛时我以为选择一个索引位置,默认就选择了它的所有倍数位置,导致不会做。其实只要排序,然后每个位置的方案数就是它左边的数选或不选的方案数,这样可以保证不会重复计算,所以快速幂计算方案数(可以在遍历的时候计算方案数),再乘以得分累加到答案即可。
1 | private static final int MOD = 998244353; |
方法一:内向基环树
对于每个 \(i\),建立一条从 \(i\) 指向 \(a_{i}\) 的边,最终会得到多个内向基环树。规则一:入度为零的节点不会被选择;规则二:如果一个节点的所有入度节点都被选择,那么它不会被选择;规则三:如果一个节点不被选择,那么它指向的节点会被选择。(以上规则可以使用拓扑序 + 染色法实现)
如图,数组为 \([2,3,4,5,6,3,3,4]\),剩余索引 \([1,5,7,8]\),剩余元素 \([2,6,3,4]\),选择索引 \([2,6,3,4]\)。应用规则一,得出索引 \([1,7,8]\) 不被选择;应用规则三,得出索引 \([2,3,4]\) 被选择;应用规则二,得出索引 \([5]\) 不被选择;应用规则三,得出索引 \([6]\) 被选择。
特殊情况,如果内向基环树没有环外节点,无法应用上述规则,那么需要单独对环交替染色,若此时环的长度为奇数,则没有解,其他情况均有解。也就是说,当且仅当内向基环树没有环外节点,且环的长度为奇数时,无解。
1 | public static void solve() { |
方法二:外向基环树
好像有建立外向基环树的解法,没时间看,在此。
数学性质,\([1,n]\) 中能被 \(m\) 整除的数有 \(\lfloor \frac{n}{m}\rfloor\) 个。
1 | class Solution { |
排序 + 贪心。
1 | class Solution { |
方法一:动态规划
今天脑子有点笨啊,本来做出来了,但是我将 dp
的初始值设置为 Integer.MAX_VALUE
,将 dfs
不满足条件时的返回值也设置为该值,而判断是否记忆化的条件也设置为该值,所以所有不满足条件的方案都没有记忆化上。
我经常会写出从上到下记忆化的代码,但是每次都比从下到上的记忆化慢,经过分析,原因如下:从下到上的记忆化,只要该节点计算过,就会直接返回;而从上到下的记忆化,只有在当前节点的值比记忆化的值大时,才会直接返回,也就是说,这样的代码只会将所有大于记忆化的值的方案给剪枝掉,所有小于记忆化的值的方案会重复计算。
1 | class Solution { |
方法二:动态规划
时间复杂度 \(O(n)\),空间复杂度 \(O(1)\) 的解法,其实比赛时第一眼差不多就想到这种解法,但是没有细想。
1 | class Solution { |
挺简单一道题,T3 卡太久,脑子短路没时间做这题。对于每一位,每次操作其实就是交换两个数之间的 \(0\) 和 \(1\),我们应该总是把 \(1\) 交换到更大的数上,这样平方和最大。所以统计每一位的 \(1\) 的个数,贪心的组合成最大的数,然后取平方加入答案即可。
1 | class Solution { |