题解链接

非常厉害的一道题。

考虑无解是什么情况? R 的个数超过 \(2^{n-1}\)

先考虑如何判定。从前往后考虑,如果遇到一个 B,那么如果后面有 R,就选最靠前的 R,否则选最靠后的一个 B.如果遇到 R,就选最靠后的一个 B

但是这个判定很繁琐。我们考虑求出一个合法序列,使得他的 B 尽量靠后。设长度为 \(2^i\) 的 B 尽量靠后的串为 \(t_i\),那么 \(t_0=\)R,考虑从 \(t_{i-1}\) 扩展到 \(t_i\).

遍历 \(t_{i-1}\),那么遇到一个 R 的时候,他匹配的是最远的 B,\(t_i\) 增加 R。遇到一个 B 的时候,他匹配最近的 R,所以增加 BR,剩下的用 B 补全即可。

然后有一个结论.考虑原串中前 \(2^{n-1}\) 个,设第 \(i\) 个 B 在 \(g_i\) 出, \(t_n\) 第 \(i\) 个 B 在 \(h_i\) 处。当且仅当 \(\forall i,g_i\le h_i\),串合法。

感性理解一下,如果原串有个 B 在 \(t_n\) 的 B 的后面,那么他就匹配不到 R

所以最终答案就是原串的 B 和 \(t_n\) 的 B 的位置差加起来就行了。

// LUOGU_RID: 139274680
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,m,k,g[N],c;
long long ans;
char s[N],t[19][N];
int main()
{
scanf("%d%s",&n,s+1);
for(int i=1;s[i];i++)
if(s[i]=='R')
++c;
if(c>(1<<n-1))
return puts("-1"),0;
t[0][1]='R';
for(int i=1;i<=n;i++)
{
m=0;
for(int j=1;j<=(1<<i-1);j++)
{
if(t[i-1][j]=='B')
t[i][++m]='B',t[i][++m]='R';
else
t[i][++m]='R';
}
while(m<(1<<i))
t[i][++m]='B';
}
m=0;
for(int i=1;i<=(1<<n);i++)
if(t[n][i]=='B')
g[++m]=i;
for(int i=1;i<=(1<<n);i++)
{
if(s[i]=='B')
{
++k;
if(k>m)
break;
ans+=max(i-g[k],0);
}
}
printf("%lld",ans);
}

[ARC169E] Avoid Boring Matches的更多相关文章

  1. Knuth-Morris-Pratt Algorithm

    Today , 第一次学习KMP Algorithm,其中好多地方还是不能理解的透彻,本文将进一步对 KMP Algorithm 进行学习,搞清楚其中的思想…… First , KMP Algorit ...

  2. 使用SDNN (space displacement neural network)进行多字体手写识别

    手写单字体的识别,在看过卷积神经网络的mnist例子之后,很容易实现,那么如何实现多字体的同时识别呢? 如下图 LeCun大神所用的是SDNN space displacement neural ne ...

  3. How to detect and avoid memory and resources leaks in .NET applications

    By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...

  4. HDOJ 3518 Boring counting

    SAM基本操作 拓扑寻求每个节点  最左边的出现left,最右边的出现right,已经有几个num ...... 对于每个出现两次以上的节点.对其所相应的一串子串的长度范围 [fa->len+1 ...

  5. hdu3518 Boring counting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3518 题目: Boring counting Time Limit: 2000/1000 MS ...

  6. HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)

    Boring counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. Why should I avoid blocking the Event Loop and the Worker Pool?

    Don't Block the Event Loop (or the Worker Pool) | Node.js https://nodejs.org/en/docs/guides/dont-blo ...

  8. keil MDK error: L6236E: No section matches selector - no section 错误

    今天板子刚到,新建的第一个工程就报错了. .\Objects\cse.sct(7): error: L6236E: No section matches selector - no section t ...

  9. 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题

    解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题http ...

  10. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

随机推荐

  1. 《深入理解Java虚拟机》读书笔记: 类加载器

                                                             类加载器   虚拟机设计团队把类加载阶段中的"通过一个类的全限定名来获取描述 ...

  2. 【LaTeX】制作 PPT(更新中)

    目录 Beamer 模板 特性 frame 与 slide \pause itemize 中的尖括号 <strat-end> 参考资料 Beamer 模板 PPT 推荐用 Beamer 模 ...

  3. ETL之apache hop系列2-hop web安装和入门

    前言 在Docker安装apache hop 首先确保Docker已经安装和运行Java 11 JDK 安装文档参考:https://blog.csdn.net/Chia_Hung_Yeh/artic ...

  4. Mybatis插件功能

    1 插件的作用 在Mybatis执行SQL的生命周期中,会使用插件进行埋点,主要包括Executor.StatementHandler.ParameterHandler和ResultSetHandle ...

  5. 数据可视化【原创】vue+arcgis+threejs 实现流光立体墙效果

    本文适合对vue,arcgis4.x,threejs,ES6较熟悉的人群食用. 效果图: 素材: 主要思路: 先用arcgis externalRenderers封装了一个ExternalRender ...

  6. 如何将项目打包上传到NuGet服务器?

    作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn 前言 在我写[在.NET Framework中使用RocketMQ(阿里云版)]这篇博客的时候,因为封 ...

  7. DesignPattern-part1

    title: "modern C++ DesignPattern-Part1" date: 2018-04-03T16:06:33+08:00 lastmod: 2018-04-0 ...

  8. Llama2-Chinese项目:2.1-Atom-7B预训练

      虽然Llama2的预训练数据相对于第一代LLaMA扩大了一倍,但是中文预训练数据的比例依然非常少,仅占0.13%,这也导致了原始Llama2的中文能力较弱.为了能够提升模型的中文能力,可以采用微调 ...

  9. Solution -「ZJOI 2014」力

    Descrption Link. 对于每一个 \(i\),求出: \[\sum_{j=1}^{i-1}\frac{a_{j}}{(i-j)^{2}}-\sum_{j=i+1}^{n}\frac{a_{ ...

  10. dp_ppi转光纤模块连接200PLC组态王通信案例

    DP_PPI转光纤模块连接200PLC组态王光纤通信在冷却塔控制系统案例 现场背景介绍: 西门子200 CPU226PLC通过兴达易控dp转光纤模块在200PLC系统中ppi转光纤实现PCL与组态王2 ...