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

时间:2023-01-30 12:01:32

http://codeforces.com/contest/962/problem/E

E. Byteland, Berland and Disputed Cities
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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<c<b) is not connected to this cable, where aa, bb and cc are simultaneously coordinates of the cities aa, bb and cc.

Input

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

The following nn lines contains an integer xixi and the letter cici (−109≤xi≤109−109≤xi≤109) — the coordinate of the city and its type. If the city belongs to Byteland, cici equals to 'B'. If the city belongs to Berland, cici equals to «R». If the city is disputed, cici 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 (cici='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 (cici='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.

Examples
input
Copy
4
-5 R
0 P
3 P
7 B
output
Copy
12
input
Copy
5
10 R
14 B
16 B
21 R
32 R
output
Copy
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=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.

思路:

第一感觉,就是把所有的R与P链接起来,然后在把B连接起来,遇到p时,p与p之间的距离就不算了,可是如果是PRP这种还要不要算呢?看来我还需要再读一遍题。

嗯,读题完毕!顺便说一句,上一题,也就是D题,是个傻逼题,只不过我这个傻逼比它更傻逼,所以没有做出来。对于这题,PRP这种结构,连接B时,P与P还是要再连接一次;感觉应该不是很难写,两个遍历时间也就是2*n,也就是4*1e5,应该跑的完(题目的2s让我有些害怕),数据大小达到了1e9,应该是要long long的,结果我想用unsigned long long来存,好吧,就是它了!

#include<iostream>
using namespace std;
struct node
{
int x;
char p;
}a[200086];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].x>>a[i].p;
}
unsigned long long ans=0;
for(int i=1;i<n;i++){
if(a[i].p==a[i-1].p&&(a[i].p=='R'||a[i].p=='P')){ans+=(a[i].x-a[i-1].x);}
else if(a[i].p=='P'&&a[i-1].p=='R'){ans+=(a[i].x-a[i-1].x);}
else if(a[i].p=='R'&&a[i-1].p=='P'){ans+=(a[i].x-a[i-1].x);}
}
for(int i=1;i<n;i++){
if(a[i].p==a[i-1].p&&a[i].p=='B'){ans+=(a[i].x-a[i-1].x);}
else if(a[i].p=='P'&&a[i-1].p=='B'){ans+=(a[i].x-a[i-1].x);}
else if(a[i].p=='B'&&a[i-1].p=='P'){ans+=(a[i].x-a[i-1].x);}
}
cout<<ans<<endl;
}

没有考虑有敌国拦在中间的情况,真是该打!

写了老半天,还是wa了,但是我的代码是没有问题的,是我把题目理解错了

#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int x;
char p;
}r[150000],b[150000],s[150000];
int main()
{
int n; int tr,tb;cin>>n;
//cout<<"n="<<n<<endl;
tr=tb=0;
for(int i=0;i<n;i++){
//cout<<i<<endl;
scanf("%d %c",&s[i].x,&s[i].p);
if(s[i].p=='R'){r[tr++]=s[i];}
else if(s[i].p=='B'){b[tb++]=s[i];}
else if(s[i].p=='P'){r[tr++]=s[i];b[tb++]=s[i];}
}
unsigned long long ans;
ans=0;
for(int i=1;i<tr;i++){
ans+=(r[i].x-r[i-1].x);
}
for(int i=1;i<tb;i++){
ans+=(b[i].x-b[i-1].x);
}
for(int i=1;i<n;i++){
if(s[i].p==s[i-1].p&&s[i].p=='P'){
ans-=(s[i].x-s[i-1].x);
}
}
cout<<ans<<endl; }

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

  1. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; E&period; Byteland&comma; Berland and Disputed Cities(贪心)

    E. Byteland, Berland and Disputed Cities time limit per test2 seconds memory limit per test256 megab ...

  2. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; D&period; 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 &lpar;Rated for Div&period; 2&rpar;F - Simple Cycles Edges

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

  4. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; 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 &lpar;Rated for Div&period; 2&rpar; B

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

  6. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; A

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

  7. D&period; Merge Equals(from Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar;)

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

  8. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar;

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

  9. C&Tab; Make a Square Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; &lpar;暴力枚举,字符串匹配&rpar;

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

随机推荐

  1. 用例设计工具PICT — 输入组合覆盖

    1 成对测试简介 成对测试(Pairwise Testing)又称结对测试.两两测试,是一种正交分析的测试技术.成对组合覆盖这一概念是Mandl于1985年在测试Aad编译程序时提出来的.是当不可能遍 ...

  2. 10&period;python中的序列

    本来说完字符串.数字.布尔值之后,应该要继续讲元祖.列表之类的.但是元祖和列表都属于序列,所以有必要先讲讲python的序列是什么. 首先,序列是是Python中最基本的数据结构.序列中的每个元素都分 ...

  3. C&num;基础精华05(正则表达式,)

    正则表达式 . 任意一个字符 除了\n以外的 []  [0-9]       [0-9a-zA-Z] |  或   [0-9]|[a-z] ()   提升优先级别   分组 ([a]|[0-9])[0 ...

  4. 编译安装mysql-server5&period;6&period;32手记

    起因: CentOS内置源里面默认安装的Mysql-server太老旧,不支持一些新方法的调用,需要重新安装一个新版本 老版本里面有数据,不想卸载,想保留库和软件本身 机器上没有运行Docker,而且 ...

  5. C&num;综合揭秘——细说多线程(二)

    /* 异步写入 FileStream中包含BeginWrite.EndWrite 方法可以启动I/O线程进行异步写入. public override IAsyncResult BeginWrite ...

  6. unity 时间转换方式

    第一种  计时器的写法 带有调用系统时间 using System; using System.Collections; using System.Collections.Generic; using ...

  7. sklearn-adaboost

    sklearn中实现了adaboost分类和回归,即AdaBoostClassifier和AdaBoostRegressor, AdaBoostClassifier 实现了两种方法,即 SAMME 和 ...

  8. python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio

    摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...

  9. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  10. Hadoop Pipes

    [Hadoop Pipes] 1.MapContext的getInputSplit()可以用于获取当前mapper所对象的文件路经,也就是Pipes中,没有InputSplit接口/对象. 2.在Pi ...