当前位置:网站首页>2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
2022-08-05 00:22:00 【Rain Sure】
题目链接
题目大意
给定两个长度为 n n n的排列 a , b a,b a,band a length consisting of two permutations 2 ∗ n 2*n 2∗n的数组 c c c,Take a number from the leftmost end of either of the two permutations and place it at a time c [ i ] c[i] c[i]里,Please construct c c c数组的方案数.
题解
I thought about linearity during the gameDP的做法,感觉很牛逼.后来看题解,Found that everyone is doing better...尤其是jianglysimulation Dafa,让我大受震撼!!!
设 f [ i ] [ j ] [ k ] f[i][j][k] f[i][j][k] 表示第 i i i个数从 j j jArray selected, i − 1 i - 1 i−1个数从 k k kArray selected,An additional array is also required g g gRecord which array was selected.具体转移看代码~ ( 0 < = j < = 1 ) ( 0 < = k < = 1 ) (0 <= j <= 1) (0 <= k <= 1) (0<=j<=1)(0<=k<=1)
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
#define x first
#define y second
#define endl '\n'
const int inf = 1e9 + 10;
const int maxn = 300010, M = 2000010;
const int mod = 998244353;
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
int h[maxn], e[M], w[M], ne[M], idx;
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, -1, 0, 1};
int cnt;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int n;
int a[maxn], b[maxn], c[maxn * 2];
int f[maxn * 2][2][2];
int g[maxn * 2][2][2];
int main()
{
int t; scanf("%d", &t);
while(t --)
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++) scanf("%d", &b[i]);
for(int i = 1; i <= n * 2; i ++) scanf("%d", &c[i]);
memset(f, 0, sizeof f); memset(g, 0, sizeof g);
if(c[1] == a[1] && c[2] == a[2]) {
f[2][0][0] = 1;
g[2][0][0] = 2;
}
if(c[1] == a[1] && c[2] == b[1]){
f[2][1][0] = 1;
g[2][1][0] = 1;
}
if(c[1] == b[1] && c[2] == a[1]){
f[2][0][1] = 1;
g[2][0][1] = 1;
}
if(c[1] == b[1] && c[2] == b[2]){
f[2][1][1] = 1;
g[2][1][1] = 0;
}
for(int i = 3; i <= n * 2; i ++){
// f[i][0][0]
int id0 = g[i - 1][0][0], id1 = g[i - 1][0][1];
// cout << id0 << ' ' << id1 << endl;
if(a[id0 + 1] == c[i]) f[i][0][0] = (f[i][0][0] + f[i - 1][0][0]) % mod, g[i][0][0] = id0 + 1;
if(a[id1 + 1] == c[i]) f[i][0][0] = (f[i][0][0] + f[i - 1][0][1]) % mod, g[i][0][0] = id1 + 1;
// f[i][0][1]
id0 = g[i - 1][1][0], id1 = g[i - 1][1][1];
if(a[id0 + 1] == c[i]) f[i][0][1] = (f[i][0][1] + f[i - 1][1][0]) % mod, g[i][0][1] = id0 + 1;
if(a[id1 + 1] == c[i]) f[i][0][1] = (f[i][0][1] + f[i - 1][1][1]) % mod, g[i][0][1] = id1 + 1;
// f[i][1][0]
id0 = g[i - 1][0][0], id1 = g[i - 1][0][1];
if(b[(i - id0)] == c[i]) f[i][1][0] = (f[i][1][0] + f[i - 1][0][0]) % mod, g[i][1][0] = id0;
if(b[i - id1] == c[i]) f[i][1][0] = (f[i][1][0] + f[i - 1][0][1]) % mod, g[i][1][0] = id1;
// f[i][1][1]
id0 = g[i - 1][1][0], id1 = g[i - 1][1][1];
if(b[i - id0] == c[i]) f[i][1][1] = (f[i][1][1] + f[i - 1][1][0]) % mod, g[i][1][1] = id0;
if(b[i - id1] == c[i]) f[i][1][1] = (f[i][1][1] + f[i - 1][1][1]) % mod, g[i][1][1] = id1;
}
int res = 0;
for(int i = 0; i < 2; i ++){
for(int j = 0; j < 2; j ++){
res = (res + f[n * 2][i][j]) % mod;
}
}
printf("%d\n", res);
}
return 0;
}
边栏推荐
- 2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
- "Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
- 2022杭电多校第三场 L题 Two Permutations
- Couple Holding Hands [Greedy & Abstract]
- 动态上传jar包热部署
- 【LeetCode】双指针题解汇总
- 进程间通信和线程间通信
- oracle创建用户
- Handwritten Distributed Configuration Center (1)
- jenkins send mail system configuration
猜你喜欢

leetcode经典例题——单词拆分

MAUI Blazor 权限经验分享 (定位,使用相机)

jenkins send mail system configuration

在线中文姓名生成工具推荐

leetcode: 266. All Palindromic Permutations
![[LeetCode] Summary of Matrix Simulation Related Topics](/img/80/bd71ca5211cce5805909015a642893.jpg)
[LeetCode] Summary of Matrix Simulation Related Topics

10 种常见的BUG分类

【LeetCode】矩阵模拟相关题目汇总

Modelers experience sharing: model study method

Three tips for you to successfully get started with 3D modeling
随机推荐
Raw and scan of gorm
软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
软件测试面试题:请你分别画出 OSI 的七层网络结构图和 TCP/IP 的四层结构图?
《MySQL入门很轻松》第2章:MySQL管理工具介绍
tiup status
#yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
leetcode: 266. All Palindromic Permutations
软件测试面试题:做好测试计划的关键是什么?
leetcode:267. 回文排列 II
tiup uninstall
Huggingface入门篇 II (QA)
软件测试面试题:您以往所从事的软件测试工作中,是否使用了一些工具来进行软件缺陷(Bug)的管理?如果有,请结合该工具描述软件缺陷(Bug)跟踪管理的流程?
【unity编译器扩展之模型动画拷贝】
2 用D435i运行VINS-fusion
ARC129E Yet Another Minimization 题解 【网络流笔记】
软件测试面试题:LoadRunner 分为哪三个模块?
关于使用read table 语句
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
SV 类的虚方法 多态
First, the basic concept of reptiles