ABC267 - C,D,E,F Solutions

C - Index × A(Continuous ver.)

Problem Statement

You are given an integer sequence of length .

Find the maximum value of for a contiguous subarray of of length .

Constraints

Solution

不妨令 ,其中 。直接计算是 的,需要进行优化。

首先可以考虑 的关系,会发现 ,于是简单前缀和就解决了。

另外也可以直接求,对 都求前缀和, 即为两者做差的形式,此处不再赘述。

Implementation

D - Index × A(Not Continuous ver.)

Problem Statement

You are given an integer sequence of length .

Find the maximum value of for a (not necessarily contiguous) subsequence of length of .

Constraints

Solution

和上题不一样的地方在于 的选择可以不连续,不难想到可以通过 dp 进行求解,于是令 表示 中,选择了 个数时 最大的值。故转移方程为

Implementation

可能为负数,需要注意边界条件。

E - Erasing Vertices 2

Problem Statement

You are given a simple undirected graph with vertices and edges. The -th edge connects Vertices and . Vertex has a positive integer written on it.

You will repeat the following operation times.

We define the cost of the entire operations as the maximum of the costs of the individual operations. Find the minimum possible cost of the entire operations.

Constraints

Solution

Minimize the maximum. 经典二分答案,需要注意删掉一个点时会导致相邻点的代价变小,所以开一个队列,在删掉后将相邻满足条件的点加到队列里。

Implementation

F - Exactly K Steps

Problem Statement

You are given a tree with vertices. The vertices are numbered , and the -th () edge connects Vertices and .

We define the distance between Vertices and on this tree by the number of edges in the shortest path from Vertex to Vertex .

You are given queries. In the -th () query, given integers and , print the index of any vertex whose distance from Vertex is exactly . If there is no such vertex, print -1.

Constraints

Solution

需要知道一个性质:令直径两端点为 ,则对于树中的任意节点 的路径与 的路径中必有一条为 点出发可达的最长路径,此处性质在两边dfs求直径的方法中被证明过。

于是先求出直径,然后分别以 为根建两棵树,使用树上倍增找点。

另:官方题解中把询问离线下来, 地解决了,回来补下这个做法。

Implementation