思路:

http://codeforces.com/blog/entry/58869

实现:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = ;
int t[MAXN], n;
char c[MAXN];
int get_sum(int i, vector<int> & v, char color)
{
int maxn = , last = v[i];
for (int j = v[i] + ; j <= v[i + ]; j++)
{
if (c[j] == color || c[j] == 'P')
{
if (t[j] - t[last] > maxn) maxn = t[j] - t[last];
last = j;
}
}
return last == v[i] ? : t[v[i + ]] - t[v[i]] - maxn;
}
int main()
{
while (cin >> n)
{
int p_start = -, p_end = -, r_start = -, r_end = -, b_start = -, b_end = -;
vector<int> v;
for (int i = ; i < n; i++)
{
cin >> t[i] >> c[i];
if (c[i] == 'P')
{
p_end = i;
if (p_start == -) p_start = i;
v.push_back(i);
}
else if (c[i] == 'R')
{
r_end = i;
if (r_start == -) r_start = i;
}
else
{
b_end = i;
if (b_start == -) b_start = i;
}
}
ll ans = ;
if (p_start == -)
{
if (r_start != -) ans += t[r_end] - t[r_start];
if (b_start != -) ans += t[b_end] - t[b_start];
cout << ans << endl;
continue;
}
if (r_start != -)
{
if (r_start < p_start) ans += t[p_start] - t[r_start];
if (r_end > p_end) ans += t[r_end] - t[p_end];
}
if (b_start != -)
{
if (b_start < p_start) ans += t[p_start] - t[b_start];
if (b_end > p_end) ans += t[b_end] - t[p_end];
}
for (int i = ; i < v.size() - ; i++)
{
int tmp_R = get_sum(i, v, 'R'), tmp_B = get_sum(i, v, 'B');
if (!tmp_R && !tmp_B) { ans += t[v[i + ]] - t[v[i]]; continue; }
int minn = * (t[v[i + ]] - t[v[i]]);
int tmp = tmp_R + tmp_B + t[v[i + ]] - t[v[i]];
minn = min(minn, tmp);
ans += minn;
}
cout << ans << endl;
}
return ;
}

CF962E 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) E. Byteland, Berland and Disputed Cities(贪心)

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

  3. 【CF962E】Byteland, Berland and Disputed Cities

    Portal! ---> 几句话题意 数轴上面有三种点(B点,R点,P点),现在要将其中的某些点连起来,满足将所有B点去掉之后,所有P点和R点都连通&将所有R点去掉之后,所有B点和P点都 ...

  4. codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)

    Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...

  5. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  6. Codeforces Round #346 (Div. 2)E - New Reform(DFS + 好题)

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  8. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

  9. Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

    E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...

随机推荐

  1. Node安装及搭建简单HTTP服务器

    注:本文安装系统为mac,windows及其他系统下载对应安装包 ,mac下载后的安装包为apk文件,windows为msi文件. 安装 1.在网上下载node安装包,官方网站2.双击下载文件,按步骤 ...

  2. 修改STM32库函数中的晶振值

    STM32F407的库文件中默认晶振值为25MHz,若外接晶振8MHz,则需修改以下几个地方: 1)修改HSE_VALUE的值 将#define HSE_VALUE ((uint32_t)250000 ...

  3. linux子系统的初始化_subsys_initcall()

    http://my.oschina.net/u/572632/blog/305492#OSC_h1_3

  4. linux初级学习笔记七:linux用户管理,密码和组命令详解!(视频序号:04_1)

    本节学习的命令: 用户管理命令:useradd,userdel,id,finger,usermod,chsh,chfn,passwd,pwck, 组管理命令:groupadd,groupmod,gro ...

  5. Oracle中如何用SQL检测字段是否包括中文字符

    用Oracle的编码转换的函数Convert实现,测试后可行. SQL> select *  2    from (select 'abcd' c1 from dual  3          ...

  6. MySQL 优化之 EXPLAIN 关键字

    MySQL查询优化之explain的深入解析 0. 准备 首先执行如下的 sql 语句: CREATE TABLE IF NOT EXISTS `article` (`id` int(10) unsi ...

  7. [Selenium] 如何使 InternetExplorerDriver 每次启动的端口不会随机变化

    InternetExplorerDriver 在不指定任何参数的情况下,启动监听端口会随机变化.如果需要保证其端口固定不变,可通过InternetExplorerDriverService 达到目的. ...

  8. 奶牛排序——RMQ

    [问题描述]奶牛在熊大妈的带领下排成了一条直队.显然,不同的奶牛身高不一定相同……现在,奶牛们想知道,如果找出一些连续的奶牛,要求最左边的奶牛 A 是最矮的,最右边的 B 是最高的,且 B 高于 A ...

  9. JAVA泛型与可变参数

    泛型的引入: 早期的Object类型可以接收任意的对象类型,但是在实际的使用中,会有类型转换的问题.也就存在这隐患,所以Java提供了泛型来解决这个安全问题. 格式: 泛型类:public class ...

  10. MongoDB复制集安全认证

    之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ...