Atcoder 记录

参考来源:本文大多数 solution 都节选自官方题解。

ABC389

G - Odd Even Graph

You are given a positive even integer \(N\) and a prime number \(P\).
For \(M = N-1, \ldots, \frac{N(N-1)}{2}\), solve the following problem.
How many undirected connected simple graphs with \(N\) vertices labeled from \(1\) to \(N\) and \(M\) edges satisfy this: the number of vertices whose shortest distance from vertex \(1\) is even is equal to the number of vertices whose shortest distance from vertex \(1\) is odd? Find this number modulo \(P\).

sol
ll t[N][N][N*N];// 本层 j 个点仅向上一层 i 连边,本层间不连边共有 k 条边的方案数。
ll g[N][N][N*N];// 本层 j 个点仅向上一层 i 连边,加上本层间的边共有 k 条边的方案数。
ll f[2][N][N][N][N*N]; //层数,奇数层点数,偶数层点数,本层有 l 个点,共 k 条边的方案数

ABC391

G - Many LCS

You are given a lowercase English string \(S\) of length \(N\) and an integer \(M\). For each \(k=0,1,\cdots,N\), solve the following problem:

  • There are \(26^M\) lowercase English strings of length \(M\). Among these, find the number, modulo 998244353, of strings whose LCS with \(S\) has length exactly \(k\).

\(1 \leq N \leq 10, 1 \leq M \leq 100\)

sol

Review how to find the length of an LCS of T and S.Taking a closer look at the transitions, we notice the following properties of the DP array:

\[dp_{i,0}=0 \]

\[dp_{i,j+1}-dp_{i,j} \in [0,1] \]

Thus, there are only \(dp_i\) possible patterns for \(2^{|S|}\).

We use this fact to solve the original problem.

Let \(DP_{i,dp\_array}\) be the number of strings \(T\) of length \(i\) such that \(dp\_array\) is the DP array \(dp_i\) when finding an LCS of \(T\) and \(S\).

The updates on \(dp\_array\) as \(i\) increases can be found according to the transitions of the DP for LCS described above.

There are \(O(2^N)\) states for \(DP_i\), and computing transitions of \(dp\_array\) costs \(O(\sigma N)\), so the overall complexity is \(O(NM\sigma 2^N)\) (where \(\sigma\) is the size of alphabet). By precalculating the transitions, the complexity can be reduced to \(O(NM2^N)\).

ABC392(Unrated)

F - Insert

There is an empty array \(A\). For \(i = 1,2,\ldots,N\), perform the following operation in order:

  • Insert the number \(i\) into \(A\) so that it becomes the \(P_i\)-th element from the beginning.
    Output the final array \(A\) after all operations have been completed.

赛时只会平衡树 /ll

G - Fine Triplets

For integers \(A, B, C\) ( \(A < B < C\) ), if they satisfy \(B-A = C-B\), then \((A, B, C)\) is called a fine triplet.
You are given a set of \(N\) distinct positive integers \(S = \{ S_1, S_2, \dots, S_N \}\). Find the number of fine triplets \((A, B, C)\) with \(A, B, C \in S\).

sol

Transposing the terms in \(B-A = C-B\), we get \(2B=A+C\).
First, let us count the number of integer pairs \((A,C)\) with \(A,C \in S\) and \(A+C=k\).

Note that the coefficient of the each term in the right hand side is no greater than \(10^6\), so one may find the convolution modulo \(998244353\) (which we can directly obtain with the convolution function in ACL).

ABC394

这场过了 ABCDFG。获得了 ARC 资格,但是不知道获没获得 ARC 实力。

E - Palindromic Shortest Path

We have a directed graph with \(N\) vertices, numbered \(1, 2, \ldots, N\).Information about the edges is given by \(N^2\) characters \(C_{1, 1}, C_{1, 2}, \ldots, C_{1, N}, C_{2, 1}, \ldots, C_{N, N}\). Here, each \(C_{i, j}\) is either a lowercase English letter or -. If \(C_{i, j}\) is a lowercase English letter, then there is exactly one directed edge from vertex \(i\) to vertex \(j\) labeled \(C_{i, j}\). If \(C_{i, j}\) is -, there is no edge from vertex \(i\) to vertex \(j\).

For each integer pair \((i, j)\) with \(1 \leq i, j \leq N\), answer the following question:

Among all (not necessarily simple) paths from vertex \(i\) to vertex \(j\) whose concatenation of labels on the edges forms a palindrome, what is the length of the shortest such path? If there is no such path, the answer is \(-1\).

sol

For a lowercase English letter \(c\) and a palindrome \(S\), the string obtained by concatenating \(c\), \(S\), and \(c\) in this order forms a palindrome. Conversely, a string of length at least two is a palindrome if and only if the first and the last characters are the same, and the string obtained by removing the first and last characters still forms a palindrome.

Using these properties, the problem can be formalized as a shortest path problem on a (directed) graph.

Let \(G\) be a graph with \(N^2\) vertices \(\lbrace (1,1), (1,2), \ldots, (1, N), (2, 1), \ldots, (N, N) \rbrace\). We will decide the edges so that vertex \((i, j)\) corresponds to a path from vertex \(i\) to vertex \(j\) in the graph given in the problem statement.

For convenience, add another vertex \(S\) to \(G\) for it to have \(N^2 + 1\). Then the answer to the original problem for an integer pair \((i, j)\) is the length of a shortest path from \(S\) to \((i, j)\) in the graph with edges added as follows:

  • An edge from \(S\) to \((i, i)\) of weight \(0\) for each \(i\).
  • An edge from \(S\) to \((i, j)\) of weight \(1\) for each \((i, j)\) with \(C_{i, j} \neq\) -.
  • An edge from \((i, j)\) to \((k, l)\) of weight \(2\) for each \((i, j, k, l)\) with \(C_{k, i} \neq\) -, \(C_{j, l} \neq\) -, \(C_{k, i} = C_{j, l}\).

The edges of the first kind represent the palindrome of length \(0\), and those for the second represent those of length \(1\). The edges of the third kind correspond to adding the same character at the beginning and the end of a palindrome to obtain a new palindrome with the length increased by two.

\(G\) has \(O(N^2)\) vertices, each of degree \(O(N^2)\), so it has \(O(N^4)\) edges, allowing \(O(N^4)\)-time BFS (Breadth-First Search) to find the answer.

Note that although the edges are weighted, one can still find the shortest distances by processing those with weight \(0\) and \(1\) first.

G - Dense Buildings

这题过了,但赛后很多天才发现做麻烦了。

ARC194

第一场 ARC,unrated,只过 AB。

C - Cost to Flip

You are given two integer sequences of length \(N\), \(A = (A_1, A_2, \ldots, A_N)\) and \(B = (B_1, B_2, \ldots, B_N)\), each consisting of \(0\) and \(1\).

You can perform the following operation on \(A\) any number of times (possibly zero):

  1. First, choose an integer \(i\) satisfying \(1 \leq i \leq N\), and flip the value of \(A_i\) (if the original value is \(0\), change it to \(1\); if it is \(1\), change it to \(0\)).
  2. Then, pay \(\sum_{k=1}^N A_k C_k\) yen as the cost of this operation.

Print the minimum total cost required to make \(A\) identical to \(B\).

WA-code
ll nw=0;
For(i,1,ce)
{
    int p1=lower_bound(d+1,d+1+cd,e[i])-d;
    int p2=lower_bound(d+1,d+1+cd,-e[i])-d;
    ll del=(p2-p1)*e[i]+(pred[p1-1]+nw+e[i]+sum)+(pred[p2-1]+nw+sum);
    if(del>0) break;
    nw+=e[i],ans+=del;
}
write(ans),End;

但其实是不能直接 break 的。

AC-code
ll nw=0,res=ans;
For(i,1,ce)
{
    int p1=lower_bound(d+1,d+1+cd,e[i])-d;
    int p2=lower_bound(d+1,d+1+cd,-e[i])-d;
    ll del=(p2-p1)*e[i]+(pred[p1-1]+nw+e[i]+sum)+(pred[p2-1]+nw+sum);
    nw+=e[i],ans+=del,res=min(res,ans);
}
write(res),End;

D - Reverse Brackets

You are given a valid parenthesis sequence \(S\) of length \(N\). You can perform the following operation any number of times:

  • Choose a contiguous substring of \(S\) that is a valid parenthesis sequence, and reverse it.

Here, reversing the substring of \(S\) from the \(l\)-th character to the \(r\)-th character means the following:

  • For every integer \(i\) satisfying \(l \leq i \leq r\), simultaneously replace \(S_i\) with ) if \(S_{l+r-i}\) is (, and with ( if \(S_{l+r-i}\) is ).(Note that reversing here is different from the usual definition of reversing.)

Find the number, modulo \(998244353\), of distinct strings \(S\) that you can have at the end of the process.

hint

将括号序列建树,区间 DP。

翻转操作等价于相邻的合法子串可以 swap,即若干并列合法子串可以重排。

Hash。

E - Swap 0^X and 1^Y

ABC397

AB 各一发罚时,不知道为什么场上连 D 题都不会。

D - Cubes

You are given a positive integer \(N\). Determine whether there exists a pair of positive integers \((x,y)\) such that \(x^3 - y^3 = N\). If such a pair exists, print one such pair \((x,y)\).

sol

\((a-b)^3 \leq (a-b)(a^2+b^2+ab) = N\)

G - Maximize Distance

You are given a directed graph with \(N\) vertices and \(M\) edges. It is guaranteed that vertex \(N\) is reachable from vertex \(1\).

Initially, all edges have weight \(0\). Choose exactly \(K\) out of the \(M\) edges and change their weights to \(1\). Maximize the shortest distance from \(1\) to \(N\).

\(2 \leq N \leq 30,1 \leq K \leq M \leq 100\)

hint

binary-search, flows

分层图。

ARC195

本来要 Unrated 打这场的,但是摆了所以直接没参加,赛后补(?开)题。BC 一车罚时(其中 B 题手动 unique 并统计次数,结果统计前没排序)。

D - Swap and Erase

There is a sequence \(A\). You can perform two types of operations any number of times in any order:

  • Let \(K\) be the length of \(A\) just before the operation. Choose an integer \(i\) such that \(1 \leq i \leq K-1\), and swap the \(i\)-th and \((i+1)\)-th elements of \(A\).

  • Let \(K\) be the length of \(A\) just before the operation. Choose an integer \(i\) such that \(1 \leq i \leq K\) and all the values from the \(1\)-st through the \(i\)-th elements of \(A\) are equal, and delete all the elements from the \(1\)-st through the \(i\)-th of \(A\).

Find the minimum number of operations required to make \(A\) empty. \(N \leq 2e5\)

hint

There is an optimal solution where each element of A is swapped at most once.

E - Random Tree Distance

For an integer sequence \(A = (A_2,A_3,\ldots,A_N)\) and an integer sequence \(P=(P_2, P_3, \ldots ,P_N)\) where \(1 \leq P_i < i\), define the weighted tree \(T(P)\) with \(N\) vertices, rooted at vertex \(1\), as follows:

  • For each \(i\) \((2 \leq i \leq N)\), the parent of \(i\) is \(P_i\), and the weight of the edge between \(i\) and \(P_i\) is \(A_i\).

You are given \(Q\) queries. The \(i\)-th query is as follows:

  • You are given integers \(u_i\) and \(v_i\). For each of the possible \((N-1)!\) sequences \(P\), take the tree \(T(P)\) and consider the distance between vertices \(u_i\) and \(v_i\) in this tree. Output the sum, modulo \(998244353\), of these distances over all \(T(P)\).
sol

\[ \mathbb{E}[d_P(u,v)] = E_u + E_v - 2\cdot \mathbb{E}[depth_P(LCA_P(u,v))], \]

\[ E_v = \begin{cases} 0 & (v = 1)\\ A_v + \frac{1}{v-1}\sum_{i=1}^{v-1}{E_i} & (2\leq v\leq N) \end{cases} \]

The Expected Depth of the LCA Does Not Depend on \(v\) (!)

By separating the case \(P_{u+1} = u\) from \(P_{u+1} \neq u\), one obtains the following recursion for \(L_u\):

\[ \begin{aligned} L_u &= \mathbb{E}[depth_P(LCA_P(u,u+1))]\\ &= \frac{1}{u}\left(\mathbb{E}[depth_P(u)] + \sum_{i=1}^{u-1}\mathbb{E}[depth_P(LCA_P(u,i))]\right)\\ &= \frac{1}{u}\left(E_u + \sum_{i=1}^{u-1} L_i\right). \end{aligned} \]

ABC399

E - Replace

You are given a positive integer \(N\) and two strings \(S\) and \(T\), each of length \(N\) and consisting of lowercase English letters.

Determine whether it is possible to make \(S\) identical to \(T\) by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required.

  • Choose two lowercase English letters \(x, y\) and replace every occurrence of \(x\) in \(S\) with \(y\).

交了好多发罚时,后来一直只错一个点,赛后发现没考虑基环树根节点自环的情况(这个时候应该把它当成树而非基环树)。

ABC400

F - Happy Birthday! 3

There is a circular cake that has been cut into \(N\) equal slices by its radii.

Each piece is labeled with an integer from \(1\) to \(N\) in clockwise order, and for each integer \(i\) with \(1 \leq i \leq N\), the piece \(i\) is also referred to as piece \(N + i\).

Initially, every piece’s color is color \(0\). You can perform the following operation any number of times:

  • Choose integers \(a\), \(b\), and \(c\) such that \(1 \leq a, b, c \leq N\). For each integer \(i\) with \(0 \leq i < b\), change the color of piece \(a+i\) to color \(c\). The cost of this operation is \(b + X_c\).

You want each piece \(i\) (for \(1 \leq i \leq N\)) to have color \(C_i\). Find the minimum total cost of operations needed to achieve this.

\(N \leq 400\)

其实是见过的题,但为什么两发罚时就放弃呢(赛后还交了 inf 发罚时/wul)

我甚至在赛时突发奇想觉得不断环成链支持 \(l>r\) 非常有意思而且省空间(其实应该是等价的,但为什么要这么搞我自己也不清楚。

以及在 DP 转移时没有枚举每个同色点,而是直接转移了最近的同色点,这样不一定最优。

code

妥协了,断环为链真好写。

	For(len,2,(n>>1)) For(l,1,n-len+1)
	{
		int r=l+len-1;
		f[l][r]=f[l][r-1]+x[c[r]]+1;
		For(k,l,r-1) if(c[k]==c[r])
			f[l][r]=min(f[l][r],f[l][k]+(k+1<=r-1?f[k+1][r-1]:0)+r-k);
	}
    ll res=inf; n>>=1;
	For(l,1,n) res=min(res,f[l][l+n-1]);

有听到别的做法,之后再补。

G - Patisserie ABC 3

\(N\) 种蛋糕,蛋糕 \(i\) 的美观度、美味度、人气度分别为 \(X_i\)\(Y_i\)\(Z_i\)

高桥君计划将蛋糕无重复地组成 \(K\) 对进行销售。当蛋糕 \(a_i\) 与蛋糕 \(b_i\) 组成一对时,该对的价格定义为 \(\max(X_{a_i} + X_{b_i},\ Y_{a_i} + Y_{b_i},\ Z_{a_i} + Z_{b_i})\)

请计算这 \(K\) 对蛋糕价格总和的可能最大值。\(N \leq 100000\)

sol

问题具有凸性,因此可以凸二分。

计算的时候我考虑的是“每选一个附加的权值”,这个东西不一定是整数,因为每选一对多出来的权值一定是整数,所以前者只能保证是 \(0.5\) 的整数倍。

一种是二分斜率的时候考虑所有 \(0.5\) 的倍数,另一种是将原值和附加值(斜率)都先乘 \(2\)

ABC404

因为放学了最后一题没写完/ll

posted @ 2025-02-20 20:07  wanggk  阅读(117)  评论(0)    收藏  举报