跳到正文

题解

Solution of CF1641D Two Arrays

tder约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年8月8日
收录
2025年12月12日
更新
2026年11月22日
篇幅
约 4 分钟
题目
CF1641D

版本

  1. v1当前

你可以浏览这个题解的中文版本。

I think this is a great approach. As a training contest, problem F is much easier than problem E, and is the one of the the only three problems with data in the contest. That is our MX (Dream Bear), an OI training institution in China.

When I see this kind of constraint, I tend to think of bitset, which leads to the following two ideas.

Approach 1: Consider nn bitsets bib_i of size n×mn\times m that maintain which numbers appear in each row. So enumerate (i,j)(i,j) and update the answer to the pair if ∣bi∪bj∣=m×2|b_i\cup b_j|=m\times2. The time complexity and space complexity are O(n3mw)\mathcal{O}(\frac{n^3m}w).

Approach 2: Consider n×mn\times m bitsets bib_i of size nn that maintain on which lines each number has appeared. For all (i,j)(i,j) that satisfies the condition there must be i∉baj,ki\not\in b_{a_{j,k}} for all kk. From this, think of enumerating ii and taking the complement tt of the union set of all bai,kb_{a_{i,k}}, then for legitimate jj there is j∈tj\in t. Since the answer is required to be minimal, each row can be sorted by wiw_i, and the optimal jj is lowbit(t)\text{lowbit}(t). Time complexity O(nlog⁡n+n2mw)\mathcal{O}(n\log n+\frac{n^2m}w), space complexity O(n2mw)\mathcal{O}(\frac{n^2m}w).

Approach 1 is certainly not promising, and is not even as good as the plain analog O(n2m)\mathcal{O}(n^2m). But we also wanted to use bitset to solve this problem, so we considered optimizing approach two. Observe that this can be a waste of time and space if a number appears only once in all rows, i.e., ∣bx∣=1|b_x|=1. This example reminds us that it is better to calculate the contribution of xx directly for fewer occurrences, and consider maintaining ∣bx∣≤nm|b_x|\le\sqrt{nm} directly in a vector instead of a bitset.

This can be done with time complexity O(nlog⁡n+nm+(nm−nm)2nw)\mathcal{O}(n\log n+nm+(nm-\sqrt{nm})^2\frac nw) and space complexity O(nnmw+nm)\mathcal{O}(\frac{n\sqrt{nm}}w+nm).

CodeForces Submission.

原文
存档评论4 条抓取于