A11712 | Byteland, Berland and Disputed Cities
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The cities of Byteland and Berland are located on the axis $Ox$ . In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $Ox$ 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 $Ox$ . It is technically possible to connect the cities $a$ and $b$ with a cable so that the city $c$ ( $a < c < b$ ) is not connected to this cable, where $a$ , $b$ and $c$ are simultaneously coordinates of the cities $a$ , $b$ and $c$ .
- 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 $Ox$ . It is technically possible to connect the cities $a$ and $b$ with a cable so that the city $c$ ( $a < c < b$ ) is not connected to this cable, where $a$ , $b$ and $c$ are simultaneously coordinates of the cities $a$ , $b$ and $c$ .
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 2 \cdot 10^{5}$ ) — the number of cities.
The following $n$ lines contains an integer $x_i$ and the letter $c_i$ ( $-10^{9} \le x_i \le 10^{9}$ ) — the coordinate of the city and its type. If the city belongs to Byteland, $c_i$ equals to 'B'. If the city belongs to Berland, $c_i$ equals to «R». If the city is disputed, $c_i$ equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
The following $n$ lines contains an integer $x_i$ and the letter $c_i$ ( $-10^{9} \le x_i \le 10^{9}$ ) — the coordinate of the city and its type. If the city belongs to Byteland, $c_i$ equals to 'B'. If the city belongs to Berland, $c_i$ equals to «R». If the city is disputed, $c_i$ equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
输出格式
Print the minimal total length of such set of cables, that if we delete all Berland cities ( $c_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 ( $c_i$ ='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
输入输出样例
输入 #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 = 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, 32$ , so to connect them you need two cables of length $11$ and $11$ . The cities of Byteland have coordinates $14$ and $16$ , so to connect them you need one cable of length $2$ . Thus, the total length of all cables is $11 + 11 + 2 = 24$ .
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, 32$ , so to connect them you need two cables of length $11$ and $11$ . The cities of Byteland have coordinates $14$ and $16$ , so to connect them you need one cable of length $2$ . Thus, the total length of all cables is $11 + 11 + 2 = 24$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted