CF962E Byteland, Berland and Disputed Cities
思路:
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的更多相关文章
- 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 ...
- 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 ...
- 【CF962E】Byteland, Berland and Disputed Cities
Portal! ---> 几句话题意 数轴上面有三种点(B点,R点,P点),现在要将其中的某些点连起来,满足将所有B点去掉之后,所有P点和R点都连通&将所有R点去掉之后,所有B点和P点都 ...
- 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 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- 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 ...
- 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 ...
- CF 191C Fools and Roads lca 或者 树链剖分
They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...
- 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 ...
随机推荐
- java 获取路径
1.利用System.getProperty()函数获取当前路径:System.out.println(System.getProperty("user.dir"));//user ...
- Uboot中start.S源码的指令级的详尽解析【转】
本文转载自:http://www.crifan.com/files/doc/docbook/uboot_starts_analysis/release/html/uboot_starts_analys ...
- POJ3281 Dining —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- 从零开始徒手撸一个vue的toast弹窗组件
相信普通的vue组件大家都会写,定义 -> 引入 -> 注册 -> 使用,行云流水,一气呵成,但是如果我们今天是要自定义一个弹窗组件呢? 首先,我们来分析一下弹窗组件的特性(需求): ...
- fullcalendar小结
最近做的项目需要一个日程插件,在网上找了一些插件觉的fullcalendar 比较好用,总结一下以备后用. 效果图如下: var calendar = null; function ShowCalen ...
- 实现自定义xib和storyboard的加载,
一:加载xib 1.分别创建xib,.h .m文件继承自UIView. 在xib上绑定类名. 或者创建文件的时候直接勾选xib 2.在控制器中调用类方法 jyq52787网盘/ios/潭州学院/iO ...
- python中PIL.Image和OpenCV图像格式相互转换
PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane.j ...
- Android 动态注册 亮屏、息屏广播
/***************************************************************************** * Android 动态注册 亮屏.息屏广 ...
- VS 一些配置设置
/************************************************************************ * VS 一些配置设置 * 说明: * 最近要用到C ...
- 【Codeforces 757B】 Bash's big day
[题目链接] 点击打开链接 [算法] 若gcd(s1,s2,s3....sk) > 1, 则说明 : 一定存在一个整数d满足d|s1,d|s2,d|s3....,d|sk 因为我们要使|s|尽可 ...