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 ...
随机推荐
- 配置webpack中externals来减少打包后vendor.js的体积
在日常的项目开发中,我们会用到各种第三方库来提高效率,但随之带来的问题就是打包后的vendor.js体积过大,导致加载时空白页时间过长,给用户的体验太差.为此我们需要减少vendor.js的体积,从本 ...
- YTU 2452: 麦克劳林用于函数求值
2452: 麦克劳林用于函数求值 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 12 题目描述 泰勒公式是一个用函数在某点的信息描述其附近取值的公式.如果函数足够光滑的话 ...
- 包、修饰符、内部类、匿名内部类(java基础知识十)
1.package关键字的概述及作用 * A:为什么要有包 * 将字节码(.class)进行分类存放 * B:包的概述 * * C:包的作用 * 包名要定义在第一行, ...
- linux初级学习笔记九:linux I/O管理,重定向及管道!(视频序号:04_3)
本节学习的命令:tr,tee,wc 本节学习的技能: 计算机的组成 I/O管理及重定向 管道的使用 知识点九:管理及IO重定向(4_3) 计算机组成: 运算器.控制器: CPU 存储器:RAM ...
- iOS——多线程编程详细解析
基本定义: 程序:由代码生成的可执行应用.(例如QQ.app) 进程:一个正在运行的程序可以看做是一个进程. (例如:正在运行的QQ 就是一个进程),进程拥有独立运行所需要的全部资源. 线程: 程序中 ...
- codeforces 459 B.Pashmak and Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/459/B 题目意思:有 n 朵 flowers,每朵flower有相应的 beauty,求出最大的beau ...
- Perl解析JSON数据精解
简介:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...
- 机器学习 Hidden Markov Models 3
Viterbi Algorithm 前面我们提到过,HMM的第二类问题是利用HMM模型和可观察序列寻找最有可能生成该观察序列的隐藏变量的序列.简单来说,第一类问题是通过模型计算生成观察序列的概率,而第 ...
- 并不对劲的bzoj4650:loj2083:uoj219:p1117:[NOI2016]优秀的拆分
题目大意 "优秀的拆分"指将一个字符串拆分成AABB的形式 十次询问,每次给出一个字符串S(\(|S|\leq3*10^4\)),求它的所有子串的优秀的拆分的方案数之和 题解 此题 ...
- react之fetch请求json数据
Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到