思路:

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. mysql表分区 partition

    表分区 partition 当一张表的数据非常多的时候,比如单个.myd文件都达到10G, 这时,必然读取起来效率降低. 可不可以把表的数据分开在几张表上? 1: 从业务角度可以解决.. (分表,水平 ...

  2. ReactMotion Demo8 分析

    链接 首先通过spring函数Motion的style参数, 传入Motion Component, 计算style的过程: const style = lastPressed === i & ...

  3. [RK3288]PMU配置(RK808)【转】

    本文转载自:http://www.javashuo.com/content/p-6398007.html 硬件原理 pmic 电路原理 平台概述 RK808 PWM 介绍 驱动分析 dts 驱动流程 ...

  4. 出现Insufficient space for shared memory file错误解决

    今天在linux下敲命令,出现上面的错误,原来是临时文件目录(/tmp)下的空间不够了,df一看/下100%了.

  5. 百度地图API--信息窗口

    信息窗口 -----纯文本信息窗口 为了更方便的提示用户,在地图的指定的地方添加文本信息的窗口,给用户直观展示信息,下面是如何添加一个纯文本的信息窗口. 下面是添加一个文本窗口: /* * 添加纯文本 ...

  6. Watir: 如果安装sougou浏览器,有可能导致Watir不能工作

    如果安装了搜狗,Watir::Browser.new并不一定能打开新的IE浏览器.这种情况下,必须卸载搜狗浏览器,当然,attach,find方法还是可以用的

  7. Github--开源代码仓库式系统(转)

    要了解Github,我们首先要知道Git,Git是管理代码的工具,写代码不是件轻松的事儿,一个人写的时候已经不轻松了,一群人写就更不轻松了,但这世界上很多事都是怎么不轻松怎么来的,大部分人都会和别人一 ...

  8. 字符指针unsigned char *ch_p

    指向类型为unsigned char的指针变量叫字符指针. 例如: unsigned char ch = 'a'; unsigned char *ch_p = &ch;那么指针变量ch_p就是 ...

  9. 什么是位、字节、字、KB、MB?

    1. 位(bit)是计算机里最小的数据单位,每一位的状态只能是0或者1 2. 字节(Byte)      1Byte = 8 bit      它是存储空间的基本计量单位,1byte可以存储一个英文字 ...

  10. C#文件读写参考

    string s = ""; string[] files = Directory.GetFiles(@"E:\u3d_warcraft\logic\logic\Batt ...