跳到正文

题解

Solution: CF1641D Two Arrays

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

文章信息

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

版本

  1. v1当前

CF1641D Two Arrays *2700

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

I think that’s a pretty awesome approach.

Start by sorting by wiw_i and consider that if we already have a set of jj minimal legitimate solutions (wi,wj)(i<j)(w_i, w_j)(i < j), a set of more optimal solutions (wu,wv)(w_u, w_v) must satisfy u<iu < i and j≤vj\le v.Notice that both pointers move in one direction, so we just have the excellent practice of moving this double pointer quickly after finding a set of legal solutions to find the optimal solution.

To move the right pointer, we need to look up a section of prefixes that form a legal solution to the right pointer.Notice that the left pointer moves in one direction, so all we need to know is whether there is a legal position in the prefix, which is the smallest legal position at p+1p + 1 after moving the left pointer to a prefix pp that does not have a legal position.

Now the question remains, how to quickly find how many of the sets {S}\{S\} intersect a specified set TT.Our direct enumeration of the occurrences of a subset of TT as a subset of all SS won’t work because there will be multiple subsets of a set that produce duplicate contributions, and we need a way to remove the redundant contributions.

That is, we need to construct a function F(T)F(T) such that ∑T∈SF(T)=1\sum_{T\in S}F(T) = 1, and this could not be simpler to construct directly as a tolerance coefficient, i.e., F(T)=(−1)∣T∣F(T) = (-1)^{\left|T\right|}.

So now the way to find how many of the sets {S}\{S\} intersect a given set TT is to enumerate all the subsets of TT, count how many times the subset occurs, multiply by the tolerance factor, and sum.Calculating how many times a subset occurs can be done with a hash or a trie, but why not use a trie if you’re going to choose a deterministic algorithm of inclusion and exclusion.

Time complexity O(nm2m)O(nm2^m).

CodeForces Submission .

原文
存档评论3 条抓取于