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. HttpServletResponse对象(转)

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  2. RocketMQ源码学习--消息存储篇

    转载. https://blog.csdn.net/mr253727942/article/details/55805876 1.序言 今天来和大家探讨一下RocketMQ在消息存储方面所作出的努力, ...

  3. 前端vue组件传参

    ## 路由传参 """ 转跳: <router-link :to="'/course/'+course.id">{{course.name ...

  4. 11.vim常用操作

    :set nu在命令模式下显示行号 进入输入模式 新增 (append) a :从光标所在位置後面开始新增资料,光标後的资料随新增资料向後移动. A:从光标所在列最後面的地方开始新增资料.   插入 ...

  5. 第十二章 ZYNQ-MIZ701 PL中断请求

      本篇文章主要介绍外设(PL)产生的中断请求,在PS端进行处理. 在PL端通过按键产生中断,PS接受到之后点亮相应的LED. 本文所使用的开发板是Miz701 PC 开发环境版本:Vivado 20 ...

  6. .Net C# RSA签名和验签

    帮助类 using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace ...

  7. C# 使用Emit实现动态AOP框架 (一)

    目  录 C# 使用Emit实现动态AOP框架 (一) C# 使用Emit实现动态AOP框架 (二) C# 使用Emit实现动态AOP框架 (三) C# 使用Emit实现动态AOP框架 进阶篇之异常处 ...

  8. vue-动态路由+动态组件+动态页面

    动态路由 路由组件是vue-router 动态路由即从后端请求路由信息,然后转化生成路由信息.所以这里的关键是不会提前知道什么菜单对应什么组件,因此路由声明的时候不再是写死的组件,而是可替换的动态路径 ...

  9. JavaScript笔记(5)

    1.DOM操作 常用的DOM操作 document.getElementById(id); //返回指定id的元素,通用 document.getElementsByTagName(tagName); ...

  10. 一秒钟教会你如何 使用jfreechart制作图表,扇形图,柱形图,线型图,时序图,附上详细代码,直接看效果

    今天有小伙伴问到我怎么使用jfreeChat生成图标,去年就有一个这方便的的总结,今天再遇到,就总结出来,供大家参考: 第一个: 创建柱状图,效果图如下: 柱状图代码如下: package cn.xf ...