AGC044C Strange Dance
在2020年A卷省选day2t2有类似操作trie的技巧。
显然是建一棵三叉trie树,代表0/1/2
对这棵trie树,我们需要支持子树交换和全局加1
考虑第一个操作怎么做?直接打个懒标记tag记录是否交换即可,因为交换偶数次次等于没交换
现在比较困难的是第二个操作,全局加1还有进位处理,相当于就是轮换,然后1变成2,0变成1,但2变成0的时候需要继续往下递归0处理进位(相当于就是对进位的位继续第二个操作),注意需要写pushdown,和写线段树的pushdown差不多
最后统计答案的时候,记得对整颗树pushdown,因为可能有标记还没下传完,具体细节类比线段树即可,然后答案就根据字典树的 \(end[i]\) 来搞就行。
时间复杂度: \(O(N)\)
代码:
#include<bits/stdc++.h>
#define maxn 5000005
using namespace std;
template<class T>
inline T read(){
T r=0,f=0;
char c;
while(!isdigit(c=getchar()))f|=(c=='-');
while(isdigit(c))r=r*10+(c^48),c=getchar();
return f?-r:r;
}
char s[maxn];
int n,len;
int pow3[15],ans[maxn];
namespace TRIE{
int trie[maxn][3],tag[maxn],tot=1,ed[maxn];
inline void insert(int x){
int u=1,tmp=x;
for(int i=1;i<=n;i++,x/=3){
if(!trie[u][x%3])trie[u][x%3]=++tot;
u=trie[u][x%3];
}
ed[u]=tmp;
}
inline void updateS(int x){
tag[x]^=1;
swap(trie[x][1],trie[x][2]);
}
inline void pushdown(int x){
if(!tag[x])return;
updateS(trie[x][0]);
updateS(trie[x][1]);
updateS(trie[x][2]);
tag[x]=0;
}
void updateR(int x){
if(!x)return;
pushdown(x);
int tmp=trie[x][2];
trie[x][2]=trie[x][1];
trie[x][1]=trie[x][0];
trie[x][0]=tmp;
updateR(trie[x][0]);
}
void calc(int x){
if(!x)return;
pushdown(x);
calc(trie[x][0]);
calc(trie[x][1]);
calc(trie[x][2]);
}
void getans(int x){
int u=1,tmp=x;
for(int i=1;i<=n;i++,x/=3)
u=trie[u][x%3];
ans[ed[u]]=tmp;
}
}
void work(){
n=read<int>();
scanf("%s",s+1);
len=strlen(s+1),pow3[0]=1;
for(int i=1;i<=n;i++)
pow3[i]=pow3[i-1]*3;
for(int i=0;i<pow3[n];i++)TRIE::insert(i);
for(int i=1;i<=len;i++){
if(s[i]=='S')TRIE::updateS(1);
else TRIE::updateR(1);
}
TRIE::calc(1);
for(int i=0;i<pow3[n];i++)
TRIE::getans(i);
for(int i=0;i<pow3[n];i++)
printf("%d ",ans[i]);
puts("");
}
int main(){
work();
return 0;
}
AGC044C Strange Dance的更多相关文章
- CCF统一省选 Day2 题解
此题解是教练给我的作业,AK了本场比赛的人,以及认为题目简单的人可以不必看 T1 算法一 暴力枚举对信号站顺序的不同排列,然后对代价取\(\min\)即可. 时间复杂度\(O(m! \cdot n)\ ...
- ATcoder Grand Contest总结
最前面: AT的题都很有思维难度,总结一下一些AT的常规操作 1.对于有操作的题目,如果正面推不行的话考虑倒推,将操作转化,寻找更好的性质 2.模型转化,看到某一种的计算的式子,需要考虑有没有更简化的 ...
- UVA 1291 十四 Dance Dance Revolution
Dance Dance Revolution Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
- [LA] 2031 Dance Dance Revolution
Dance Dance Revolution Time limit: 3.000 seconds Mr. White, a fat man, now is crazy about a game nam ...
- BZOJ 1305: [CQOI2009]dance跳舞 二分+最大流
1305: [CQOI2009]dance跳舞 Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲 ...
- timus 1175. Strange Sequence 解题报告
1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...
- CF719C. Efim and Strange Grade[DP]
C. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- ACM : HDU 2899 Strange fuction 解题报告 -二分、三分
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
随机推荐
- 2019-8-31-dotnet-动态代理魔法书
title author date CreateTime categories dotnet 动态代理魔法书 lindexi 2019-08-31 16:55:58 +0800 2019-06-02 ...
- Excel根据名称批量导入图片
一. 效果 二.方法一 在Excel中如果内容 ="<table><img src=""D:\文档\常用文件\图片导入测试\"&A2& ...
- STM32定时器原理
一.简介 不同的芯片定时器的数量不同,STM32F10x中一共有11个定时器,其中2个高级控制定时器,4个普通定时器和2个基本定时器,以及2个看门狗定时器和1个系统嘀嗒定时器. 基本定时器:TIM6. ...
- win10的开机启动文件夹
1.在C:\Users(用户)\Administrator(当前用户名)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs(「开始」菜单)\P ...
- Mybatis Plus的@TableId标签
@TableId1.如果数据库字段设成user_id在初始生成后,在代码中会变成userId,不会设置成主键使用**@TableId(value="user_id",type = ...
- Unity Linear Gamma色彩空间矫正测试
Gamma和Linear修正的问题相信网上已经有很多文章了.简单来说显示器的颜色输出不是线性的,根据硬件参数和输出颜色 信息拟合曲线是x^2.2,因此会使用一个x^0.45曲线将其拟合回线性. 因为0 ...
- uniapp 微信支付,小程序支付,支付宝支付问题汇总
背景介绍 uni-app 可以转微信小程序或直接打包 APP ,支付模块使用统一方法 uni.requestPayment 但是不同平台参数不同容易出现混淆错乱. 相关网站 uni-app 统一支付: ...
- 基于webapi的websocket聊天室(番外一)
上一篇我已经实现了聊天室,并且在协议中实现了4种类型的消息传输.其实还可以添加video,audio,live等等类型. 不过假如把目前的协议看作RCP1.0版的话,这个版本就只支持有限的4种消息.精 ...
- 二:nacos 的服务注册
一:怎么引用依赖 dependencyManagement:spring cloud alibaba 的版本,由于spring cloud alibaba 还未纳入spring cloud 的主版本管 ...
- win10找回Ubuntu启动项(非EasyBCD)
最近想对装在电脑上的Ubuntu进行更新,但是之前在BIOS里改了引导系统的文件,导致找不到Ubuntu启动项,EasyBCD程序也不起作用(整块硬盘Windows分区都是GPT,改BIOS也没什么用 ...