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. oracle授予权限

    CONNECT角色:    --是授予最终用户的典型权利,最基本的 CREATE    SESSION    --建立会话 RESOURCE角色:    --是授予开发人员的    CREATE    ...

  2. Python 解leetcode:3. Longest Substring Without Repeating Characters

    题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中 ...

  3. select key from table 一直出错

    key和keys  为mysql 关键字,数据库设计字段的时候尽量避免

  4. 这里除了安全,什么都不会发生!Docker镜像P2P加速之路

    1.1      问题: 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队 ...

  5. Date及DateFormat用法

    Date 与DateFormat之间的转化String <————>Date Date与Calendar 之间的转化Long<————>Date 日历小程序 Scanner i ...

  6. 远程连接windows2003桌面无法使用剪切板的有效解决方法

    远程桌面控制服务器时,无法剪切.粘贴一些东西,上网搜了一下,原来是rdpclip.exe(remote desktop clipboard)不起作用了.此程序负责管理本地机与远程服务器之间共享剪切板, ...

  7. [NOIP10.3模拟赛]3.w题解--神奇树形DP

    题目链接: 咕 闲扯: 这题考场上把子任务都敲满了,5个namespace,400行11k 结果爆0了哈哈,因为写了个假快读只能读入一位数,所以手测数据都过了,交上去全TLE了 把边分成三类:0. 需 ...

  8. JS 编程艺术

    JS艺术片段剪贴 getFullDate: function (date) { //返回 YYYY年MM月DD日 var year = month = day = ' '; if (isNaN(dat ...

  9. 前端必学TypeScript之第一弹,st基础类型!

    TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScript与JavaScript相比 ...

  10. WinPE基础知识之代码解析

    void CMyPE::OnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 // 打开一个文件夹选择对话框 CFileDialog dlg(TRUE); dlg. ...