题解链接

非常厉害的一道题。

考虑无解是什么情况? 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. 2、搭建MyBatis

    2.1.开发环境 IDE:idea 2019.2 构建工具:maven 3.8.4 MySQL版本:MySQL 5.7 MyBatis版本:MyBatis 3.5.7 MySQL不同版本的注意事项 ( ...

  2. 在centos7.X下安装Java

    发布于 2 分钟前  2 次阅读 1.创建JDK目录 mkdir /opt/jdk 2.通过finalshell或xftp将jdk上传到/opt/jdk目录中 3.cd /opt/jdk 4.解压jd ...

  3. .NET周刊【8月第4期 2023-08-27】

    国内文章 AgileConfig-1.7.0 发布,支持 SSO https://www.cnblogs.com/kklldog/p/agileconfig-170.html AgileConfig ...

  4. (null) entry in command string: null chmod 0644

    在WIndows操作系统中本地运行spark程序,报以下错误: ....(null) entry in command string: null chmod 0644 ..(后面是目的目录) 解决方法 ...

  5. [Maven] maven插件系列之maven-shade-plugin

    [Maven] maven插件系列之maven-shade-plugin 1 插件简述/Plugin Overview 1.1 定义与目的/Definition & Goals Officia ...

  6. tarjan强连通分量

    int scc[N],sc;//结点i所在scc的编号 int sz[N]; //强连通i的大小 //dfn(u)为搜到结点u时的次序编号 //low(u)为u或u的子树能够追溯到的最早的栈中节点的次 ...

  7. Node练习 | 文件管理模块使用

    功能 新建一个Project文件夹, 里面是三个新建的文件, 分别是app.js/app.css/index.html 实现步骤 fs模块中的同步和非同步 同步 等待运行完成后再运行下一步 本次练习为 ...

  8. 历时一个月,《穿透Laravel》全书完成!

    近几年来Laravel在PHP领域大放异彩,逐渐成为PHP开发框架中的中流砥柱. 这个系列的文章, 会带你一起探知Laravel框架底层的实现细节.与其他框架相比,Laravel的设计理念确实更为先进 ...

  9. MySQL 高级(进阶) SQL 语句

    MySQL 高级(进阶) SQL 语句 use gy; create table location (Region char(20),Store_Name char(20)); insert into ...

  10. Springboot项目使用Undertow替换内置Tomcat服务器,实现RESTFUL接口web应用

    Maven实例:pom.xml文件中添加更换依赖 <dependency> <groupId>org.springframework.boot</groupId> ...