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 ...
随机推荐
- SCX-4521F一体机MAC驱动
如果您想下载SCX-4521F一体机MAC驱动,请从下面的链接中下载相应驱动:MAC打印驱动:http://org.downloadcenter.samsung.com/downloadfile/Co ...
- YTU 2417: C语言习题 字符串长度
2417: C语言习题 字符串长度 时间限制: 1 Sec 内存限制: 128 MB 提交: 758 解决: 548 题目描述 写一函数,求一个字符串的长度.在main函数中输入字符串,并输出其长 ...
- COGS-2049 疯狂动物城
Description 你意外来到了一个未知的星球, 这里是一个动物乌托邦, 生活着一群拥有非凡智力的动物. 你遇到了一个叫做尼克的狐狸, 他准备给他的 GF 过生日 . 他将制作一个巨大的多层蛋糕, ...
- HihoCoder1706 : 末尾有最多0的乘积(还不错的DP)
描述 给定N个正整数A1, A2, ... AN. 小Hi希望你能从中选出M个整数,使得它们的乘积末尾有最多的0. 输入 第一行包含两个个整数N和M. 第二行包含N个整数A1, A2, ... AN. ...
- codeforces round 421 div2 补题 CF 820 A-E
A Mister B and Book Reading O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...
- 将svn的项目转移到另外一个仓库中
前几天在做一个项目的时候,因为需要,需要将Server A 上SVN仓库 repos1中的项目pro1迁移到Server B 上的SVN仓库中,首先想到的是:通过复制,但是仔细一想,这样是不可能的:然 ...
- .NETFramework:Stopwatch
ylbtech-.NETFramework:Stopwatch 1.返回顶部 1. #region 程序集 System, Version=4.0.0.0, Culture=neutral, Publ ...
- sublime 插件:Emmet
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- git只clone仓库中指定子目录
基于sparse clone变通方法 [root@vm_test backup]# mkdir devops[root@vm_test backup]# cd devops/[root@vm_test ...
- Jquery之each函数详解
最近项目被each函数坑惨了,想来还是好好整理下关于each函数的方方面面,一来方便自己查阅,二来为读者提供经验和教训,废话不多说,来看看Each函数到底是怎么坑人的. 一. 全局jQuery.eac ...