CF908F.New Year and Rainbow Roads

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Roy and Biv have a set of n points on the infinite number line.

Each point has one of 3 colors: red, green, or blue.

Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.

They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).

However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.

Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).

Help them compute the minimum cost way to choose edges to satisfy the above constraints.

Roy 和 Biv 在无限长的数轴上有 $ n $ 个点。

每个点具有以下三种颜色之一:红色、绿色或蓝色。

Roy 和 Biv 希望用若干条边将所有点连接起来。任意两个给定点之间均可连一条边,边的代价等于其所连接两点之间的距离。

他们希望以某种方式选择这些边,使得 Roy 和 Biv 都能观察到所有点是连通的(即:任意两点之间均存在一条由边构成的路径,无论直接或间接)。

但有一个限制条件:Roy 看不见红色,而 Biv 看不见蓝色。

因此,他们必须选择边,使得:若移除所有红色点,则剩余的蓝色点和绿色点仍然连通;同理,若移除所有蓝色点,则剩余的红色点和绿色点也仍然连通。

请帮助他们计算满足上述约束条件的最小总代价。

输入格式

The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points.

The next n lines will contain two tokens p__i and c__i (p__i is an integer, 1 ≤ p__i ≤ 109, c__i is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.

第一行包含一个整数 nn(1≤n≤300 0001 \leq n \leq 300\,000),表示点的数量。

接下来的 nn 行每行包含两个项 pip_i 和 cic_i(其中 pip_i 是一个整数,满足 1≤pi≤1091 \leq p_i \leq 10^9;cic_i 是一个大写英文字母,为 'R'、'G' 或 'B'),分别表示第 ii 个点的位置和颜色。'R' 表示红色,'G' 表示绿色,'B' 表示蓝色。所有位置严格递增。

输出格式

Print a single integer, the minimum cost way to solve the problem.

输出一个整数,即解决该问题的最小代价。

输入输出样例

  • 输入#1

    4
    1 G
    5 R
    10 B
    15 G

    输出#1

    23
  • 输入#2

    4
    1 G
    2 R
    3 B
    10 G

    输出#2

    12

说明/提示

In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.

在第一个样例中,最优方案是在点对 (1,2)、(1,4)、(3,4) 之间连边。它们的代价分别为 4、14 和 5。

输入解题思路,AI测评打分。不知道怎么写?

首页