OvO http://codeforces.com/contest/908/problem/F

  CF 908F

  需要注意细节的模拟题。

  如果三种颜色都存在,则记每两个相邻的G组成一个段,对每个段进行讨论。

  如果三种颜色中缺了某种或某些颜色,则特殊地进行讨论。

  如果是像 BRB 这组数据 如果把第一个B和第三个B连起来,那么中间的R也和这两个B相连了

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm> using namespace std; typedef long long ll; const int M=3e5+44;
const int INF=1e9+44; struct Node
{
int plc,tp;
} node[M]; int n; void solve()
{
ll ans=0;
int big1,big2,sum1,sum2,lst0,lst1,lst2,tmp;
lst0=lst1=lst2=-1;
sum1=sum2=0; big1=big2=-1;
for(int i=1;i<=n;i++)
{
if(node[i].tp==0)
{
if(lst1!=-1)
sum1+=node[i].plc-lst1,big1=max(big1,node[i].plc-lst1);
if(lst2!=-1)
sum2+=node[i].plc-lst2,big2=max(big2,node[i].plc-lst2);
if(lst0==-1) ans+=sum1,ans+=sum2;
else
{
ans+=sum1,ans+=sum2,ans+=node[i].plc-lst0;
if(big1==-1) big1=0;
if(big2==-1) big2=0;
tmp=big1+big2;
if(big1!=0 && big2!=0)
tmp=max(tmp,node[i].plc-lst0);
// cout<<ans<<' '<<tmp<<' '<<big1<<' '<<big2<<' '<<sum1<<' '<<sum2<<endl;
ans-=tmp;
}
lst1=lst2=-1;
sum1=sum2=0; big1=big2=-1;
lst0=node[i].plc;
}
else if(node[i].tp==1)
{
if(lst1==-1)
{
if(lst0==-1)
sum1+=0;
else
sum1+=node[i].plc-lst0,big1=max(big1,node[i].plc-lst0);
}
else
sum1+=node[i].plc-lst1,big1=max(big1,node[i].plc-lst1);
lst1=node[i].plc;
}
else
{
if(lst2==-1)
{
if(lst0==-1)
sum2+=0;
else
sum2+=node[i].plc-lst0,big2=max(big2,node[i].plc-lst0);
}
else
sum2+=node[i].plc-lst2,big2=max(big2,node[i].plc-lst2);
lst2=node[i].plc;
}
}
ans+=sum1,ans+=sum2;
printf("%I64d",ans);
} int main()
{
int cnt[3],lst[3],cst[3];
char chr[3];
scanf("%d",&n);
memset(cst,0,sizeof(cst));
memset(lst,-1,sizeof(lst));
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
{
scanf("%d%s",&node[i].plc,chr);
if(chr[0]=='G')
{
node[i].tp=0;
cnt[0]++;
if(lst[0]!=-1) cst[0]+=node[i].plc-lst[0];
lst[0]=node[i].plc;
}
else if(chr[0]=='B')
{
node[i].tp=1;
cnt[1]++;
if(lst[1]!=-1) cst[1]+=node[i].plc-lst[1];
lst[1]=node[i].plc;
}
else
{
node[i].tp=2;
cnt[2]++;
if(lst[2]!=-1) cst[2]+=node[i].plc-lst[2];
lst[2]=node[i].plc;
}
}
if(cnt[0]!=0 && cnt[1]==0 && cnt[2]==0)
{
printf("%d",cst[0]);
return 0;
}
if(cnt[0]==0)
{
if(cnt[1]==0 || cnt[2]==0)
{
printf("%d",cst[1]+cst[2]);
return 0;
}
int mn=INF,flag12,flag21;
ll ans=0;
flag12=flag21=0;
for(int i=1;i<n;i++)
{
if(node[i].tp!=node[i+1].tp && (node[i+1].plc-node[i].plc<mn))
mn=node[i+1].plc-node[i].plc;
if(node[i].tp==1 && node[i+1].tp==2)
flag12=1;
if(node[i].tp==2 && node[i+1].tp==1)
flag21=1;
}
if(flag12 && flag21)
mn=0;
ans=0ll+cst[1]+cst[2]+mn;
printf("%I64d",ans);
return 0;
}
solve();
return 0;
} /* 6
1 R
5 B
9 R
10 B
14 R
15 B */

  

Codeforces Good Bye 2017 908F F. New Year and Rainbow Roads的更多相关文章

  1. [Codeforces]Good Bye 2017

    A - New Year and Counting Cards #pragma comment(linker, "/STACK:102400000,102400000") #inc ...

  2. Good Bye 2017 部分题解

    D. New Year and Arbitrary Arrangement 分析 \(dp[i][j]\) 表示已有 \(i\) 个 \(a\) 和 \(j\) 个 \(ab\) 的情况下继续构造能得 ...

  3. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  4. Good Bye 2017 A B C

    Good Bye 2017 A New Year and Counting Cards 题目链接: http://codeforces.com/contest/908/problem/A 思路: 如果 ...

  5. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  6. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  7. Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid

    F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  8. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  9. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

随机推荐

  1. 小菜鸟之liunx

    目录 第一章:Linux简介 1 Linux特点 1 CentOS 1 第二章:Linux安装 2 Linux目录结构 2 第三章:Linux常用命令 2 Linux命令的分类 3 操作文件或目录常用 ...

  2. MVCC原理 4步 什么是MVCC、事务ACID、事物隔离级别、Innodb存储引擎是如何实现MVCC的

    MVCC是来处理并发的问题,提高并发的访问效率,读不阻塞写.事物A 原子性C 一致性I 隔离性D 持久性高并发的场景下的问题脏读不可重复读幻读事物隔离级别RU读未提交 脏读/不可重复读/幻读 .不适用 ...

  3. LeetCode 第 15 场双周赛

    1287.有序数组中出现次数超过25%的元素 1288.删除被覆盖区间 1286.字母组合迭代器 1289.下降路径最小和 II 下降和不能只保留原数组中最小的两个,hacked. 1287.有序数组 ...

  4. CentOS7下使用Harbor搭建Docker私有仓库

    相关资料: Harbor官方网站:https://goharbor.io/ Harbor Github地址:https://github.com/goharbor/harbor ⒈安装Docker(必 ...

  5. ubuntu18下Docker运行springboot项目

    1.springboot项目打成jar包 mvn install 2.编写Dockerfile # 基础镜像使用java FROM java:8 # 作者 #MAINTAINER sk # VOLUM ...

  6. jsp获取Session中的值

    摘要:这个问题算是老生常谈了,我也是一段时间没弄过了,所以感觉有些忘了,就记录一下. 一.后端通过shiro在session中存储数据: // username是前台传过来的用户名 if (subje ...

  7. S02_CH04_User_IP实验Enter a post title

    S02_CH04_User_IP实验 4.1 创建IP 在之前的教程中,我们通过MIO与EMIO来控制LED,所使用的也是官方的IP,实际当中,官方提供的IP不可能涵盖到方方面面,用户需要自己编写硬件 ...

  8. 手动部署k8s-prometheus

    简介 Prometheus 最初是 SoundCloud 构建的开源系统监控和报警工具,是一个独立的开源项目,于2016年加入了 CNCF 基金会,作为继 Kubernetes 之后的第二个托管项目. ...

  9. javascript 仿jQuery的无new构造函数

    /* 匿名函数 传入 window 值全局变量成为局部变量 */ (function(window,undefined) { /* 申明一个名为jQuery 的函数*/ function jQuery ...

  10. 5.移动端自动化测试-小知识 import和from...import的区别

    一.import   1 import导入的时,需要使用模块名的限定. 举个例子,我们首先创建一个md.py文件,里面有一个函数 2 然后在1.py文件中引用这个函数. 注意,我们需要使用md.的方式 ...