当前位置:网站首页>杭电多校3 1012. Two Permutations dp*
杭电多校3 1012. Two Permutations dp*
2022-08-01 22:16:00 【Strezia】
1012
dp
题意
给出长度为 n n n 的全排列 p , q p,q p,q,还有一个由 p , q p,q p,q 组成的长度为 2 × n 2\times n 2×n 的序列 S S S 。
现在有一个空序列 R R R ,每次可以从 p p p 或 q q q 的开头取出一个数字并加到 R R R 的末尾,问有多少种取法使得 R = S R = S R=S 。
思路
记 d p [ x ] [ y ] dp[x][y] dp[x][y] 表示 p , q p,q p,q 中分别取前 x , y x,y x,y 位时的方案数。则 d p [ x ] [ y ] = d p [ x + 1 ] [ y ] + d p [ x ] [ y + 1 ] dp[x][y] = dp[x+1][y]+dp[x][y+1] dp[x][y]=dp[x+1][y]+dp[x][y+1] ,前者需满足 p [ x + 1 ] = s [ x + y + 1 ] p[x+1]=s[x+y+1] p[x+1]=s[x+y+1] ,后者需满足 q [ y + 1 ] = s [ x + y + 1 ] q[y+1] = s[x+y+1] q[y+1]=s[x+y+1] 。
但因为空间不够,无法开二维数组(当然也可以用 std::map,但常数比较大),所以改成 d p [ 2 × m a x n ] [ 2 ] dp[2\times maxn][2] dp[2×maxn][2] ,其中 d p [ i ] [ 0 / 1 ] dp[i][0/1] dp[i][0/1] 表示两数组一共匹配了 i i i 位且目前最后一位匹配的是 p / q p/q p/q 的方案数。转移也类似写就可以,代码如下:
int dfs(int x, int y, int t) {
if(x == n && y == n) return 1;
if(dp[x + y + 1][t] != -1) return dp[x + y + 1][t];
int ans = 0;
if(p[x + 1] == s[x+y+1] && x < n) {
ans += dfs(x + 1, y, 0);
ans %= P;
}
if(q[y + 1] == s[x+y+1] && y < n) {
ans += dfs(x, y + 1, 1);
ans %= P;
}
return dp[x+y+1][t] = ans;
}
//调用
int ans = 0;
if (p[1] == s[1])ans += dfs(1, 0, 0), ans %= P;
if (q[1] == s[1])ans += dfs(0, 1, 1), ans %= P;
这个代码看起来是 O ( n 2 ) O(n^2) O(n2) ,实际上是 O ( n ) O(n) O(n) ,因为 s s s 中每个数都只出现了两次(如果不是直接0),且在 p , q p,q p,q 中各出现一次,也就是 s s s 中同一个位置最多和两个位置(p,q中对应的位置)匹配,因为 s s s 长度为 2 × n 2\times n 2×n ,所以时间复杂度为 O ( 4 n ) O(4n) O(4n)。
代码
int n;
int dp[maxn << 1][2], p[maxn], q[maxn];
int s[maxn << 1];
int dfs(int x, int y, int t) {
if(x == n && y == n) return 1;
if(dp[x + y + 1][t] != -1) return dp[x + y + 1][t];
int ans = 0;
if(p[x + 1] == s[x+y+1] && x < n) {
ans += dfs(x + 1, y, 0);
ans %= P;
}
if(q[y + 1] == s[x+y+1] && y < n) {
ans += dfs(x, y + 1, 1);
ans %= P;
}
return dp[x+y+1][t] = ans;
}
void solve() {
cin >> n;
for(int i = 1; i <= n * 2; i++) dp[i][0] = dp[i][1] = -1;
for(int i = 1;i <= n; i++) cin >> p[i];
for(int i = 1;i <= n; i++) cin >> q[i];
for(int i = 1;i <= n * 2; i++) cin >> s[i];
int ans = 0;
if (p[1] == s[1])ans += dfs(1, 0, 0), ans %= P;
if (q[1] == s[1])ans += dfs(0, 1, 1), ans %= P;
cout << ans << endl;
}
边栏推荐
- dvwa 通关记录1 - 暴力破解 Brute Force
- 感觉自己好傻
- APP专项测试:流量测试
- Deep learning Course2 first week Practical aspects of Deep Learning exercises
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(8)毕业设计论文模板
- (翻译)按钮的对比色引导用户操作的方式
- 复现gallerycms字符长度限制短域名绕过
- Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
- 2022 版 MySQL 巅峰教程,收藏好,慢慢看
- Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
猜你喜欢

联邦学习在金融领域的发展和应用

(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程

入门数据库Days4

Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen

Prufer sequence

使用分类权重解决数据不平衡的问题

ImportError: `save_weights` requires h5py. Problem solved

Shell programming conditional statement

小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书

SOM Network 2: Implementation of the Code
随机推荐
VGUgarbage collector(垃圾回收器)的实现原理
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
SOM网络2: 代码的实现
将vim与系统剪贴板的交互使用
Analysis of the development trend of game metaverse
一种灵活的智能合约协作方式
Shell programming conditional statement
游戏元宇宙发展趋势展望分析
还在纠结报表工具的选型么?来看看这个
SAP Spartacus NgExpressEngineDecorator 的工作原理
The must-have "fishing artifact" for programmers is here!
不卷了!入职字节跳动一周就果断跑了。
罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍
Lecture 3: Several common table field data types in MySQL database
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
familiar friend
企业公众号文章写作方向:如何写出读者认可的优质内容
使用Jenkins做持续集成,这个知识点必须要掌握
Graph Theory - Strongly Connected Component Condensation + Topological Sort
数据分析面试手册《指标篇》