• 题意:给你一组不重复的序列\(a\),每次可以选择一个数删除它左边或右边的一个数,并将选择的数append到数组\(b\)中,现在给你数组\(b\),问有多少种方案数得到\(b\).

  • 题解:我们可以记录\(b_i\)在\(a_i\)中的位置,然后枚举\(b_i\),取它在\(a_i\)的位置,然后看\(a_{i-1}\)和\(a_{i+1}\)的情况,因为我们append之后必须要删除\(a_{i-1}\)和\(a_{i+1}\)中的一个,并且所有元素都是不重复的,所以\(a_{i-1}\)和\(a_{i+1}\)必然不能出现在\(b_{i+1}...b_{n}\)中,而当我们append \(a_i\)之后,它也就变成了没用的数.

    所以我们可以讨论\(a_{i-1}\)和\(a_{i+1}\)的情况,假如它们两个都在\(b_{i+1}...b_{n}\)中出现,那么我们肯定不能构造出\(b\),直接\(ans=0\)然后结束,假如它们两个中有一个在\(b_{i+1}...b_n\)中出现,那么我们删除另外一个,因为删除的方案是固定的,所以对答案没有贡献,假如它们两个都没有出现,因为\(a_{i-1},a_i,a_{i+1}\)都是没有用的数,所以我们可以删去\(a_{i-1}\)或\(a_{i+1}\)中的任意一个,并且\(ans*=2\).

    具体实现我们可以用双向链表,并且标记\(b_i,...,b_n\),每次操作后将\(b_i\)的标记删除即可.

  • 代码:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
#define rep(a,b,c) for(int a=b;a<=c;++a)
#define per(a,b,c) for(int a=b;a>=c;--a)
const int N = 1e6 + 10;
const int mod = 998244353 ;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} struct misaka{
int pre;
int nxt;
}e[N]; int t;
int n,m;
int a[N],b[N];
int pos[N];
bool cnt[N]; void init(){
rep(i,1,n){
e[i].pre=i-1;
e[i].nxt=i+1;
}
e[1].pre=0;
e[n].nxt=0;
} void Delete(int x){
if(e[x].pre) e[e[x].pre].nxt=e[x].nxt;
if(e[x].nxt) e[e[x].nxt].pre=e[x].pre;
} int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
rep(i,1,n) cnt[i]=false;
rep(i,1,n){
cin>>a[i];
pos[a[i]]=i;
}
rep(i,1,m){
cin>>b[i];
b[i]=pos[b[i]]; //映射到a数组的位置
cnt[b[i]]=true;
} init(); //双向链表的初始化
cnt[0]=true;
int ans=1; rep(i,1,m){
if(cnt[e[b[i]].pre]){
if(cnt[e[b[i]].nxt]){
ans=0;
break;
}
else{
Delete(e[b[i]].nxt);
}
}
else{
if(cnt[e[b[i]].nxt]){
Delete(e[b[i]].pre);
}
else{
ans=ans*2%mod;
Delete(e[b[i]].nxt);
}
}
cnt[b[i]]=false;
}
cout<<ans<<'\n';
} return 0;
}

Codeforces Round #681 (Div. 1, based on VK Cup 2019-2020 - Final) B. Identify the Operations (模拟,双向链表)的更多相关文章

  1. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)【ABCDF】

    比赛链接:https://codeforces.com/contest/1443 A. Kids Seating 题意 构造一个大小为 \(n\) 的数组使得任意两个数既不互质也不相互整除,要求所有数 ...

  2. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) D. Extreme Subtraction (贪心)

    题意:有一个长度为\(n\)的序列,可以任意取\(k(1\le k\le n)\),对序列前\(k\)项或者后\(k\)减\(1\),可以进行任意次操作,问是否可以使所有元素都变成\(0\). 题解: ...

  3. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)

    题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...

  4. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) B. Saving the City (贪心,模拟)

    题意:给你一个\(01\)串,需要将所有的\(1\)给炸掉,每次炸都可以将一整个\(1\)的联通块炸掉,每炸一次消耗\(a\),可以将\(0\)转化为\(1\),消耗\(b\),问将所有\(1\)都炸 ...

  5. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) A. Kids Seating (规律)

    题意:给你一个正整数\(n\),在\([1,4n]\)中找出\(n\)个数,使得这\(n\)个数中的任意两个数不互质且不能两两整除. 题解:这题我是找的规律,从\(4n\)开始,往前取\(n\)个偶数 ...

  6. Codeforces Round 623(Div. 2,based on VK Cup 2019-2020 - Elimination Round,Engine)D. Recommendations

    VK news recommendation system daily selects interesting publications of one of n disjoint categories ...

  7. Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020 - Elimination Round, Engine)A(模拟,并查集)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; pair<]; bool cmp( ...

  8. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine)

    A. Dead Pixel(思路) 思路 题意:给我们一个m*n的表格,又给了我们表格中的一个点a,其坐标为(x, y),问在这个表格中选择一个不包括改点a的最大面积的矩形,输出这个最大面积 分析:很 ...

  9. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) C. Restoring

    C. Restoring Permutation time limit per test1 second memory limit per test256 megabytes inputstandar ...

随机推荐

  1. (二)数据源处理1-configparser读取.ini配置文件

    import osimport configparsercurrent_path =os.path.dirname(__file__)#获取config当前文件路径config_file_path = ...

  2. Python+Docker+Flask+pyecharts实现数据可视化

    1.数据加工pyecharts图实现: 数据源:本地CSV文件 ps:由于是跟生产环境做交互,生产环境指标由HSQL加工,使用存储过程挂后台定时运行,后使用python实现导出及定时分发,本地pyth ...

  3. 【Spring】Spring的事务管理 - 1、Spring事务管理概述(数据库事务、Spring事务管理的核心接口)

    Spring事务管理概述 文章目录 Spring事务管理概述 数据库事务 什么是Spring的事务管理? Spring对事务管理的支持 Spring事务管理的核心接口 Platform Transac ...

  4. (三)React Ant Design Pro + .Net5 WebApi:后端环境搭建

    一. 简介 1. 平常用的core webapi 3.1,恰逢.Net5.0正式版发布了,直接开整. 2. 先学习IdentityServer4 .Autofac.EF Core,集成到后台框架里. ...

  5. ctfhub技能树—信息泄露—目录遍历

    打开靶机 查看页面 点击后发现几个目录 于是开始查找 在2/1目录下发现flag.txt 成功拿到flag 练习一下最近学习的requests库 附上源码 #! /usr/bin/env python ...

  6. 2V升3.3V芯片,输出500MA,低功耗10uA解决方案

    2V的输入电压其实非常少,一般都是镍氢电池1.2V,干电池1.5V,来给玩具,MCU单片机,模块啊,等等供电.不过2V的供电电源或者设备确实是不常见的. 一般2V升3.3V,需要升压芯片PW5100即 ...

  7. Linux网卡没有eth0显示ens33原因以及解决办法

    原因 首先说明下eth0与ens33的关系: 目前的主流网卡为使用以太网络协定所开发出来的以太网卡 (Ethernet),因此我们 Linux 就称呼这种网络接口为 ethN (N 为数字). 举例来 ...

  8. monitor a local unix domain socket like tcpdump

    Can I monitor a local unix domain socket like tcpdump? - Super User https://superuser.com/questions/ ...

  9. 页面切换提速30%!京东商城APP首屏耗时监控及优化实践

    https://mp.weixin.qq.com/s/vIG_x1MWC33HKV1GxalVHg 原创 平台研发朱跃棕等 京东零售技术 2020-12-09 网络接口请求可以从接口结构合理性及多接口 ...

  10. 如何让淘宝不卡顿? 读写比例 动态扩容 分布式化路线 mysql 优化

    作为数据库核心成员,如何让淘宝不卡顿? https://mp.weixin.qq.com/s/l-qXV8NI6ywnUvp3S6an3g