Codeforces Good Bye 2017 908F F. New Year and Rainbow Roads
题
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的更多相关文章
- [Codeforces]Good Bye 2017
A - New Year and Counting Cards #pragma comment(linker, "/STACK:102400000,102400000") #inc ...
- Good Bye 2017 部分题解
D. New Year and Arbitrary Arrangement 分析 \(dp[i][j]\) 表示已有 \(i\) 个 \(a\) 和 \(j\) 个 \(ab\) 的情况下继续构造能得 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Good Bye 2017 A B C
Good Bye 2017 A New Year and Counting Cards 题目链接: http://codeforces.com/contest/908/problem/A 思路: 如果 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
随机推荐
- [转帖]Linux cpufreq 机制了解
Linux cpufreq 机制了解 https://www.cnblogs.com/armlinux/archive/2011/11/12/2396780.html 引用文章链接: http://w ...
- [转帖] 飞腾FT2000+ CPU的进展(2019.6)
中国长城:拟进一步收购飞腾股权,强化信息基础设施国产化平台地位 2019-06-26 09:28 http://www.sohu.com/a/323065095_100016383 今年年中的事情 浪 ...
- 【51nod】2591 最终讨伐
[51nod]2591 最终讨伐 敲51nod是啥评测机啊,好几次都编译超时然后同一份代码莫名奇妙在众多0ms中忽然超时 这道题很简单就是\(M\)名既被诅咒也有石头的人,要么就把石头给没有石头被诅咒 ...
- Linux IO模式以及select poll epoll详解
一 背景 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network ...
- 第一次安装Dev-c++编译器如何设置?Dev-c++神奇功能
安装完Dev-c++后,我们通常会根据自己的喜好调整设置,以下是个人的做法,欢迎参考. 1.字体 字体在安装界面就可以更改.默认字体是consolas,个人喜欢下面的courier new 有些字体很 ...
- Ubuntu系统降内核
本人安装的Ubuntu16.04.6系统原生内核为4.15.0,但安装的应用仅支持4.8.0以下内核,因此需要降内核.PS:降内核有风险,操作前请慎重 1.查看可用的内核 输入命令查看已经可用的内核 ...
- 模块和包,logging模块
模块和包,logging日志 1.模块和包 什么是包? 只要文件夹下含有__init__.py文件就是一个包. 假设文件夹下有如下结构 bake ├── test.py ├── __init__.py ...
- Promise.all的使用
我有一个需求如下: 有一个学生号的数组,根据这个数组,取出相关学生的所有信息.获取学生信息的接口用setTime模拟,假设每次请求需要耗时2s,代码如下 // 获取学生信息接口 function ge ...
- InnoDB意向锁的作用
https://www.zhihu.com/question/51513268?sort=created IX,IS是表级锁,不会和行级的X,S锁发生冲突.只会和表级的X,S发生冲突. 意向锁是一种快 ...
- 【转载】python format遇上花括号{}
在format string中, 大括号已经被format占用,想要使用大括号本身,该怎么办? 以下转载自这里. ============ 分割线 ============ 使用format时,字符串 ...