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 ...
随机推荐
- SAP Adobe Form 教程五 图片
前文: SAP Adobe Form 教程一 简单示例 SAP Adobe Form 教程二 表 SAP Adobe Form 教程三 日期,时间,floating field SAP Adobe F ...
- Solution Set - 线段树
A[洛谷P5787]无向图,每条边有一个出现时段,求每个单位时段图是否是二分图. B[洛谷P5227]无向图,询问删除一个边集后是否连通. C[洛谷P3733]连通无向图,边有权,加边.修改(增加的边 ...
- rails byebug
Gemfile里添加 gem 'byebug' bundle install 在要打断点的地方写 byebug byebug -h #帮助 c 放行,入下走 n 单行调适 q 退出进行 启动异步任务推 ...
- golang基础之结构体
匿名结构体 在定义一些临时数据结构等场景下还可以使用匿名结构体. 在函数体内 package main import ( "fmt" ) func main() { //方法一 v ...
- 如何去掉WordPress分类目录url链接中的category,如何处理生成的作者链接
一个新网站需要结合SEO,才能做成一个优秀的网站, ◆◆◆ 关于WordPress的分类目录url中含有category的处理办法: 1,如果是新网站这些设置需要提前做,方便以后做SEO 1.修改固定 ...
- fastposter 2.5.0 全新发布 一款电商级海报生成器
fastposter 2.5.0 全新发布 低代码海报生成器 fastposter低代码海报生成器,一分钟完成海报开发.支持Java.Python.PHP. Go.JavaScript等多种语言. v ...
- 保障升级:Splashtop 公布安全顾问委员会成员
加利福尼亚州圣何塞,2020年12月17日-远程访问和远程支持解决方案的全球领导者 Splashtop Inc. 召集了网络安全性和合规性方面的领先专家,成立了该公司的安全顾问委员会.这组顾问有助于指 ...
- Scala集合flatten操作
一层嵌套,但是flatten的要求需要List内部类型都一样, 例如都为List scala> List(List(1), List(2), List(3)).flatten res4: Lis ...
- LLM实战:LLM微调加速神器-Unsloth + LLama3
1. 背景 五一结束后,本qiang~又投入了LLM的技术海洋中,本期将给大家带来LLM微调神器:Unsloth. 正如Unsloth官方的对外宣贯:Easily finetune & tra ...
- java学习之旅(day.21)
HTML 初识HTML HTML: Hyper Text Markup Language(超文本标记语言) 超文本包括文字.图片.音频.视频.动画等 W3C标准 W3C :World Wide Web ...