E. Byteland, Berland and Disputed Cities

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Input

The first line contains a single integer n (2≤n≤2⋅105) — the number of cities.

The following n lines contains an integer xi and the letter ci (−109≤xi≤109) — the coordinate of the city and its type. If the city belongs to Byteland, ci equals to 'B'. If the city belongs to Berland, ci equals to «R». If the city is disputed, ci equals to 'P'.

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

Output

Print the minimal total length of such set of cables, that if we delete all Berland cities (ci='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 (ci='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.

Examples

inputCopy

4

-5 R

0 P

3 P

7 B

outputCopy

12

inputCopy

5

10 R

14 B

16 B

21 R

32 R

outputCopy

24

Note

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.

题意:

在坐标轴上有3种城市,分别是R,B,P,让你给一些城市连上无向边 满足这2个条件:

1、 删除所有的R节点之后,剩下的节点两两相互连通。

2、 删除所有的B节点之后,剩下的节点两两相互连通。

如果连接边的成本是两个城市的距离,请你是算出最小的距离。

思路:
对于每一个B节点,我们肯定是要让其与上一个B节点连接,R也相同, 当遇到P 节点使,有两种选择:

1、 让其与上一个B和上一个R相连。

2、让其与上一个P相连,然后删除一条最长的B-B或者R-R边。 这样一定可以满足题目要求的条件。

我们当然要选择成本最小的一个。

注意每一次选择第2个的时候,把维护的最长的B-B或者R-R边清空。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const ll inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ ll ans = 0ll; ll preb = -inf;
ll prer = -inf;
ll prep = -inf;
ll max_r = 0ll;
ll max_b = 0ll; int n;
ll pos;
char t;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin >> n;
repd(i, 1, n) {
cin >> pos >> t;
if (t == 'R' || t == 'P') {
if (prer != -inf) {
ans += pos - prer;
max_r = max(max_r, pos - prer);
}
prer = pos;
} if (t == 'B' || t == 'P') {
if (preb != -inf) {
ans += pos - preb;
max_b = max(max_b, pos - preb);
}
preb = pos;
} if (t == 'P') {
if(prep!=-inf)
{
ans+=min(0ll,-max_r-max_b+pos-prep);
}
prep=pos;
max_r=0;
max_b=0;
}
}
cout<<ans<<endl;
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities(贪心)的更多相关文章

  1. Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities

    http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...

  2. Educational Codeforces Round 42 (Rated for Div. 2) D. Merge Equals

    http://codeforces.com/contest/962/problem/D D. Merge Equals time limit per test 2 seconds memory lim ...

  3. Educational Codeforces Round 42 (Rated for Div. 2)F - Simple Cycles Edges

    http://codeforces.com/contest/962/problem/F 求没有被两个及以上的简单环包含的边 解法:双联通求割顶,在bcc中看这是不是一个简单环,是的话把整个bcc的环加 ...

  4. Educational Codeforces Round 42 (Rated for Div. 2) C

    C. Make a Square time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Educational Codeforces Round 42 (Rated for Div. 2) B

    B. Students in Railway Carriage time limit per test 2 seconds memory limit per test 256 megabytes in ...

  6. Educational Codeforces Round 42 (Rated for Div. 2) A

    A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  7. D. Merge Equals(from Educational Codeforces Round 42 (Rated for Div. 2))

    模拟题,运用强大的stl. #include <iostream> #include <map> #include <algorithm> #include < ...

  8. Educational Codeforces Round 42 (Rated for Div. 2)

    A. Equator(模拟) 找权值的中位数,直接模拟.. 代码写的好丑qwq.. #include<cstdio> #include<cstring> #include< ...

  9. C Make a Square Educational Codeforces Round 42 (Rated for Div. 2) (暴力枚举,字符串匹配)

    C. Make a Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

随机推荐

  1. Java内存泄漏分析和预防

    1. 什么是内存泄漏?有什么危害 书面说法: 内存泄漏:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 在Java中,内存泄漏就是存在一些被分配的对象,这些对象有下面两个 ...

  2. 读取PC版微信数据库(电脑版微信数据库)内容

    原始网址   https://www.cnblogs.com/Charltsing/p/WeChatPCdb.html 1.PC版微信的密钥是32位byte,不同于安卓版(7位字符串) 2.通过OD或 ...

  3. shell笔记-----常用命令积累

    set -x # 执行指令前,先输出指令#set -o xtrace  # 与set -x效果一样 set -e 在"set -e"之后出现的代码,一旦出现了返回值非零,整个脚本就 ...

  4. Go语言基本类型

    1.Go语言fmt包详解 fmt.Println() ###常用打印 fmt.Print() fmt.Printf() ###格式化 fmt.Sprintf() ###字符串拼接 a)普通占位符 占位 ...

  5. 清除表单input输入框内数据

    清除表单input输入框内数据 1. $(':input','#addVoucherType') //'#addVoucherType'表单id .not(':button') .val('') .r ...

  6. 如何将txt文件转换为带章节目录的mobi文件

    txt文件基本没什么排版可言.所以想要把txt转换为mobi文件方便阅读. 具体步骤如下: 打开txt 用notepad++打开所需要转换的txt文件.(或者使用其他的能够支持正则表达式的编辑器). ...

  7. CTF—攻防练习之SMB私钥泄露

    攻击机:192.168.32.152 靶机 :192.168.32.155 打开靶机 nmap一下 我们看到了开放了 ssh,smb,mysql这些端口,还有一个大端口 对smb服务我们可以1.使用空 ...

  8. Blender2.8基础操作

    1.Blender2.8版本快捷键方式和2.79b大致相同.2.Blender2.8操作视图的方式可以按住主键盘数字键1旁边的~符号键,然后鼠标选择需要的视图. 3.视图与基本操作: 选择方式时鼠标左 ...

  9. 面向对象及os模块、socket模块

    1.面向对象及面向过程 1)面向过程:核心过程二字,过程即解决问题的步骤,就是先干什么后干什么 基于该思想写程序就好比在这是一条流水线,是一种机械式的思维方式 优点:复杂的过程流程化 缺点:扩展性差 ...

  10. ocelot集成consul服务发现

    首先下载consul 点击这里下载 转到解压文件夹目录输入cmd命令  consul agent -dev (有时候会卡住按一下方向键上) 在浏览器中输入http://localhost:8500/u ...