CF962E.Byteland, Berland and Disputed Cities

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The cities of Byteland and Berland are located on the axis OxOx. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line OxOx there are three types of cities:

  • the cities of Byteland,
  • the cities of Berland,
  • disputed cities.

Recently, the project BNET has been launched — a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.

The countries agreed to connect the pairs of cities with BNET cables in such a way that:

  • If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables,
  • If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.

Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.

The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.

Each city is a point on the line OxOx. It is technically possible to connect the cities aa and bb with a cable so that the city cc (a<c<ba \lt c \lt b) is not connected to this cable, where aa, bb and cc are simultaneously coordinates of the cities aa, bb and cc.

比特兰和贝尔兰两座城市位于 OxOx 轴上。此外,该轴上还存在若干争议城市——在两国各自看来,这些城市均属于本国。因此,在 OxOx 轴上共有三类城市:

  • 比特兰的城市,
  • 贝尔兰的城市,
  • 争议城市。

最近,新一代计算机网络项目 BNET 启动了。当前,两国的任务是连接各自的城市,使得本国的网络保持连通。

两国约定以如下方式用 BNET 电缆连接成对的城市:

  • 若仅考虑比特兰的城市与争议城市,则在所得的城市集合中,任意城市均应可通过一条或多条电缆到达其余任意城市;
  • 若仅考虑贝尔兰的城市与争议城市,则在所得的城市集合中,任意城市均应可通过一条或多条电缆到达其余任意城市。

因此,需选出一组城市对,并用电缆连接它们,使得上述两个条件同时满足。电缆支持双向数据传输,且每条电缆恰好连接两个不同的城市。

从一座城市铺设电缆至另一座城市的成本等于两城之间的距离。请找出满足上述要求(即:比特兰城市与争议城市的子集连通,且贝尔兰城市与争议城市的子集也连通)的电缆铺设方案的最小总成本。

每座城市是 OxOx 轴上的一个点。技术上允许将坐标分别为 aa 和 bb 的两座城市 aa、bb 直接用电缆连接,即使其间存在另一座坐标为 cc(满足 a<c<ba \lt c \lt b)的城市 cc 未被该电缆连接。

输入格式

The first line contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5}) — the number of cities.

The following nn lines contains an integer xix_i and the letter cic_i (−109≤xi≤109-10^{9} \le x_i \le 10^{9}) — the coordinate of the city and its type. If the city belongs to Byteland, cic_i equals to 'B'. If the city belongs to Berland, cic_i equals to «R». If the city is disputed, cic_i equals to 'P'.

All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^{5})—— 城市的数量。

接下来的 nn 行,每行包含一个整数 xix_i 和一个字母 cic_i(−109≤xi≤109-10^{9} \le x_i \le 10^{9})—— 分别表示该城市的坐标及其类型。若该城市属于拜特兰(Byteland),则 cic_i 为 'B';若属于贝尔兰(Berland),则 cic_i 为 'R';若为争议城市,则 cic_i 为 'P'。

所有城市的坐标互不相同,且保证输入的城市按其坐标升序给出。

输出格式

Print the minimal total length of such set of cables, that if we delete all Berland cities (cic_i='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities (cic_i='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.

输出满足以下条件的电缆集合的最小总长度:

  • 若删除所有贝兰德城市(cic_i='R'),则仅通过电缆,任意剩余城市均可到达其他任意剩余城市;
  • 同样地,若删除所有比特兰德城市(cic_i='B'),则仅通过电缆,任意剩余城市均可到达其他任意剩余城市。

输入输出样例

  • 输入#1

    4
    -5 R
    0 P
    3 P
    7 B

    输出#1

    12
  • 输入#2

    5
    10 R
    14 B
    16 B
    21 R
    32 R

    输出#2

    24

说明/提示

In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be 5+3+4=125 + 3 + 4 = 12.

In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates 10,21,3210, 21, 32, so to connect them you need two cables of length 1111 and 1111. The cities of Byteland have coordinates 1414 and 1616, so to connect them you need one cable of length 22. Thus, the total length of all cables is 11+11+2=2411 + 11 + 2 = 24.

在第一个例子中,你需要将第一座城市与第二座城市连接,第二座城市与第三座城市连接,第三座城市与第四座城市连接。电缆的总长度为 5+3+4=125 + 3 + 4 = 12。

在第二个例子中,不存在争议城市,因此你需要连接所有比特兰(Byteland)的相邻城市对,以及所有贝尔兰(Berland)的相邻城市对。贝尔兰的城市坐标为 10,21,3210, 21, 32,因此连接它们需要两根长度分别为 1111 和 1111 的电缆。比特兰的城市坐标为 1414 和 1616,因此连接它们需要一根长度为 22 的电缆。于是,所有电缆的总长度为 11+11+2=2411 + 11 + 2 = 24。

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

首页