ABC238 (D-F) 题解

ABC238 D-F

D-AND and SUM

Solve the following problem for \(T\) test cases.

Given are non-negative integers \(a\) and \(s\). Is there a pair of non-negative integers \((x, y)\) that satisfies both of the conditions below?

  • \(x \text{ AND }y=a\)
  • \(x+y = s\)

Constraints

  • \(1 \leq T \leq 10^5\)
  • \(0 \leq a,s \lt 2^{60}\)
  • All values in input are integers.

Solution

解析两个条件

  • 第一个条件告诉我们\(a\)每个\(bit\)\(1\)\(x,y\)在该\(bit\)上一定为\(1\)
  • 第二个条件则可以用于验证,我们已经知道\(x,y\)都为\(1\)\(bit\),剩余的\(bit\)最多只有\(x\)或者\(y\)其中一个为\(1\),那我们考虑都在\(x\)上考虑能否分配成功即可。(假如分配到两者都为\(1\)\(bit\)则分配失败)。

D-代码

E-Range Sums

Takahashi has a secret integer sequence \(a\). You know that the length of \(a\) is \(N\).

You want to guess the contents of \(a\). He has promised to give you the following \(Q\) additional pieces of information.

  • The \(i\)-th information: the value \(a_{l_i}+a_{l_i+1}+\cdots+a_{r_i}\)

Is it possible to determine the sum of all elements in \(a\), \(a_1+a_2+\cdots+a_N\), if the \(Q\) pieces of promised information are given?

Constraints

  • \(1 \leq N \leq 2 \times 10^5\)
  • \(1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})\)
  • \(1 \leq l_i \leq r_i \leq N\)
  • \((l_i,r_i) \neq (l_j,r_j)\ (i \neq j)\)
  • All values in input are integers.

Solution

这题有两种办法,可以用DSU也可以直接建图做。

对于给定的一个信息: \([l, r]\),实际上是对于\((l-1 \leftrightarrow r)\)建立了一条无向边。

最后我们只需要判断能否从\(0\)出发抵达\(n\)即可。

可以bfs也可以dsu

E-代码

F-Two Exams

In the Kingdom of Takahashi, \(N\) citizens numbered \(1\) to \(N\) took an examination of competitive programming.
There were two tests, and Citizen \(i\) ranked \(P_i\)-th in the first test and \(Q_i\)-th in the second test. There were no ties in either test. That is, each of the sequences \(P\) and \(Q\) is a permutation of \((1, 2, \cdots, N)\).

Iroha, the president of the kingdom, is going to select \(K\) citizens for the national team at the coming world championship of competitive programming. The members of the national team must be selected so that the following is satisfied.

  • There should not be a pair of citizens \((x, y)\) where Citizen \(x\) is selected and Citizen \(y\) is not selected such that \(P_x>P_y\) and \(Q_x > Q_y\).
    • In other words, if Citizen \(y\) got a rank samller than that of Citizen \(x\) in both tests, it's not allowed to select \(x\) while not selecting Citizen \(y\).

To begin with, Iroha wants to know the number of ways to select citizens for the national team that satisfy the condition above. Find it to help her.Since this number can be enormous, print it modulo \(998244353\).

Constraints

  • All values in input are integers.
  • \(1 \le N \le 300\)
  • \(1 \le K \le N\)
  • Each of \(P\) and \(Q\) is a permutation of \((1,2,...,N)\)

Solution

这题没写出来,是一个很好的题目。

这是一个二维偏序问题,目前我了解的比较少,后续可以扩充一下。

但是,根据我之前做过的类似题目,我们需要降维,变成一维问题进行处理。

根据\(P,Q\)两个序列,生成一个新的序列\(V\),其中\(V\)序列第\(i\)个元素代表在\(P\)中排名第\(i\)位的市民在\(Q\)中的排名(也就是\(V[P[i]] = Q[i]\))。

然后进行dp,写出dp的数组表示:

dp[i][j][k] := 对于v中的前i个人,选了j个的情况下,且没选的人中Q排名最小为k时的种类。

为什么要维护\(Q\)排名最小呢?

显然,对于一个新的市民\(y\)我们在考虑选不选的时候,由于我们按\(V\)中的顺序遍历,所以对于之前没有被选的市民\(x\)一定有\(P_x < P_y\)

  • 如果\(Q_x < Q_y\) 那么,\(y\)一定不能选。
  • 如果\(Q_x > Q_y\) 那么,\(y\)可选可不选。

所以维护未被选的\(Q\)排名最小方便进行状态转移。

\[dp[prei][current][mnQ] = \left\{ \begin{array}{ll} dp[prei-1][current-1][mnQ] & current \neq k \& V[i]<mnQ\\ dp[prei-1][current][\min(mnQ, V[i])] & mnQ < V[i]\\ \end{array} \right. \]

由于我们只需要dp[i]dp[i-1]两个,所以我们可以用两个数组,swap即可。

最终时间复杂度\(\mathcal{O}(n^3)\)

F-代码

posted @ 2022-02-08 15:01  Last_Whisper  阅读(180)  评论(0编辑  收藏  举报