A11546 | New Year and Rainbow Roads
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line will contain an integer $n$ ( $1<=n<=300000$ ), 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}<=10^{9}$ , $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.
The next $n$ lines will contain two tokens $p_{i}$ and $c_{i}$ ( $p_{i}$ is an integer, $1<=p_{i}<=10^{9}$ , $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.
输出格式
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted