题解链接

非常厉害的一道题。

考虑无解是什么情况? 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. 1Nginx基础及编译安装

    1Nginx基础 1.Nginx概述 Nginx 功能介绍 Nginx(发音为"engine-x")是一个开源的高性能 HTTP和反向代理服务器.它具有以下功能: 1.静态文件服务 ...

  2. 《Kali渗透基础》02. 基本工具

    @ 目录 1:基本工具 1.1:NetCat 1.1.1:命令参数 1.1.2:示例 1.2:NCat 1.2.1:命令参数 1.2.2:示例 1.3:WireShark 1.4:TCPdump 1. ...

  3. Skynet通讯遇到的奇怪问题

    问题 最近在做一个内部通讯的服务器, 用的自带的gateserver和socketchannel做通讯, 在使用skynet.unpack或者string.unpack("XXXX" ...

  4. 「codeforces - 585E」Present for Vitalik the Philatelist

    link. 设 \(\displaystyle f(x) = \# S', s.t. S' \subseteq S, S' \neq \varnothing, \gcd(S') = x\),\(g(x ...

  5. Django框架项目——redis操作、Celery

    1-redis操作 redis介绍 redis安装 """ 1.官网下载:安装包或是绿色面安装 2.安装并配置环境变量 """ redis ...

  6. 前端三件套系例之JS——JavaScript内置方法

    文章目录 1.Number 1-1 属性 1-2 方法 2.String 2-1 属性 2-2 方法 2-3 代码 3Array 3-1 创建数组 3-2 数组特点 3-3 数组的遍历(迭代) 34 ...

  7. 是因为不同的浏览器内核吗--Could not register service workers到底是怎么回事

    什么是浏览器内核 浏览器内核(Rendering Engine),是浏览器最核心的部分. 它负责处理网页的HTML.CSS.JavaScript等代码,并将其转化为可视化的网页内容.即我们常说的对网页 ...

  8. CSS之3D翻转效果

    <!DOCTYPE html> <html> <head> <title></title> <style type="tex ...

  9. tomcat nio2源码分析

    一. 前言 ​ 最近在看tomcat connector组件的相关源码,对Nio2的异步回调过程颇有兴趣,平时读源码不读,自己读的时候很多流程都没搞明白,去查网上相关解析讲的给我感觉也不是特别清晰,于 ...

  10. openwrt使用tailscale实现内网穿透

    问题 之前一直有电信公网ip,最近发现电信公网ip被撤下来了,打电话再去要发现给的是10开头的ip,电信客服还跟我说10开头就是公网ip,= =,根本就不是,无奈使用zerotier进行打洞,把zer ...