博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Good Bye 2017: F. New Year and Rainbow Roads(模拟)
阅读量:2144 次
发布时间:2019-04-30

本文共 3682 字,大约阅读时间需要 12 分钟。

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Roy and Biv have a set of n points on the infinite number line.

Each point has one of 3 colors: red, green, or blue.

Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.

They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).

However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.

Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).

Help them compute the minimum cost way to choose edges to satisfy the above constraints.

Input

The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points.

The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.

Output

Print a single integer, the minimum cost way to solve the problem.

Examples
input
41 G5 R10 B15 G
output
23
input
41 G2 R3 B10 G
output
12

发现错误没时间改了,就差几秒钟交不上去了。。

题意:一条横轴上有n个点,每个点都有一个颜色(RGB三种颜色中的一种),你可以将任意两点连接起来,代价为两点之间的距离,问如何以最小的代价满足:①删除所有红点(R)后剩下的点全部连通;②删除所有蓝色(B)点后剩下的点全部连通

思路:

首先很显然将一个红点和一个蓝点连起来没有意义

对于没有绿点的情况:将所有红点用一条直线连起来,将所有蓝点用一条直线连起来就ok

对于有绿点的情况,对于相邻的两个绿点有且只有两种连法:

(△是绿点)

两种取最优的,然后最两边的绿点外的所有红点蓝点全部连一条线到最近的绿点即可

#include
#include
using namespace std;#define LL long longtypedef struct{ int x; int tp;}Point;Point s[300005];int Min[4], Max[4];LL a1[300005];int main(void){ LL ans; char ch; int i, n, bet, last1, last2, cnt; scanf("%d", &n); for(i=1;i<=3;i++) Min[i] = 1e9+1, Max[i] = 0; for(i=1;i<=n;i++) { scanf("%d %c", &s[i].x, &ch); if(ch=='R') s[i].tp = 1, Max[1] = max(s[i].x, Max[1]), Min[1] = min(s[i].x, Min[1]); if(ch=='G') s[i].tp = 3, Max[3] = max(s[i].x, Max[3]), Min[3] = min(s[i].x, Min[3]); if(ch=='B') s[i].tp = 2, Max[2] = max(s[i].x, Max[2]), Min[2] = min(s[i].x, Min[2]); } if(Max[3]==0) { ans = 0; if(Max[1]) ans += Max[1]-Min[1]; if(Max[2]) ans += Max[2]-Min[2]; printf("%lld\n", ans); } else { ans = Max[3]-Min[3]; if(Max[1]) { cnt = bet = 0, last1 = last2 = -1; for(i=1;i<=n;i++) { if(s[i].tp==3) { if(last2!=-1) { ans += s[i].x-s[last2].x; bet = max(bet, s[i].x-s[last2].x); } if(last1!=-1) { if(bet) a1[++cnt] = s[i].x-s[last1].x-bet; ans -= bet; } last1 = last2 = i; bet = 0; } else if(s[i].tp==1) { if(last2!=-1) { ans += s[i].x-s[last2].x; bet = max(bet, s[i].x-s[last2].x); } last2 = i; } } } if(Max[3]) { cnt = bet = 0, last1 = last2 = -1; for(i=1;i<=n;i++) { if(s[i].tp==3) { if(last2!=-1) { ans += s[i].x-s[last2].x; bet = max(bet, s[i].x-s[last2].x); } if(last1!=-1) { if(bet) a1[++cnt] += s[i].x-s[last1].x-bet; if(a1[cnt]>s[i].x-s[last1].x) ans -= a1[cnt]-s[i].x+s[last1].x; ans -= bet; } last1 = last2 = i; bet = 0; } else if(s[i].tp==2) { if(last2!=-1) { ans += s[i].x-s[last2].x; bet = max(bet, s[i].x-s[last2].x); } last2 = i; } } } printf("%lld\n", ans); } return 0;}

你可能感兴趣的文章
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>