版本比较
题解:P11482 [NordicOI 2021] Pearls
正文
增加 9 行删除 4 行
12345678910111213141516117 行未改动全部展开134135136137138139140141142#### [题目传送门](https://www.luogu.com.cn/problem/P11482)#### 思路::::error[$16\text{pts}$]{open}首先想到的就是暴力。暴力匹配 $A_i$ 和 $B_j$ 存入答案,然后查询,时间复杂度 $O(la \times lb + q)$,而题目中 $la,lb$ 最大可以达到 $2 \times 10^5$,不仅会超时,而且也无法存下 $2 \times \left(2 \times {10^5}\right)^2=8 \times 10^5$ 个字符,预估得分 $16\text{pts}$。::::::::success[$100\text{pts}$]{open}考虑二分+前缀和。我们可以使用 `set<pair<char, char>>` 来预处理每个「丑陋颜色对」,由于 $A$ 仅由小写字母组成,我们可以用 $i$ 枚举 $\texttt{a}$ 到 $\texttt{z}$ 的 $26$ 个字母,然后用 $j$ 枚举 $B$ 的每一个字母,如果非「丑陋颜色对」,就将 $i$ 与 $B_j$ 加入答案数组内。然后进行前缀和,用 $pre_i$ 表示前 $i$ 个 $A$ 中的字符贡献的总长度。最后使用 `upper_bound` 进行二分,输出答案即可。::::#### 代码::::success[AC Code]117 行未改动全部展开int idx = upper_bound(pre, pre + la + 1, x) - pre; // 二分,找到第一个大于x的pre[i]--idx; //减去1得到当前段的编号cout << ans[a[idx] - 'a'][x - pre[idx]] << endl; //输出答案}return 0;}```::::时间复杂度为 $O(26 × lb + la + q \log la)$,省略常数为 $O(lb+la+q \log la)$,可以通过此题。123456789101112131415161718192021117 行未改动全部展开139140141142143144145146147#### [题目传送门](https://www.luogu.com.cn/problem/P11482)#### 思路::::error[$16\text{pts}$]{open}首先想到的就是暴力。暴力匹配 $A_i$ 和 $B_j$ 存入答案,然后查询,时间复杂度 $O(la \times lb + q)$,而题目中 $la,lb$ 最大可以达到 $2 \times 10^5$,不仅会超时,而且也无法存下 $2 \times \left(2 \times {10^5}\right)^2=8 \times 10^{10}$ 个字符,预估得分 $16\text{pts}$。::::::::success[$100\text{pts}$]{open}考虑二分+前缀和。我们可以使用 `set<pair<char, char>>` 来预处理每个「丑陋颜色对」,由于 $A$ 仅由小写字母组成,我们可以用 $i$ 枚举 $\texttt{a}$ 到 $\texttt{z}$ 的 $26$ 个字母,然后用 $j$ 枚举 $B$ 的每一个字母,如果非「丑陋颜色对」,就将 $i$ 与 $B_j$ 加入答案数组内。然后进行前缀和,用 $pre_i$ 表示前 $i$ 个 $A$ 中的字符贡献的总长度,得:$$pre_i=\begin{cases}0, \hspace{3.5cm} i=0\\pre_{i-1}+|ans_{A_{i-1}}|,\hspace{1.07cm}i \geq 1\end{cases}$$最后使用 `upper_bound` 进行二分,输出答案即可。::::#### 代码::::success[AC Code]117 行未改动全部展开int idx = upper_bound(pre, pre + la + 1, x) - pre; // 二分,找到第一个大于x的pre[i]--idx; //减去1得到当前段的编号cout << ans[a[idx] - 'a'][x - pre[idx]] << endl; //输出答案}return 0; // 完结撒花}```::::时间复杂度为 $O(26 × lb + la + q \log la)$,省略常数为 $O(lb+la+q \log la)$,可以通过此题。