Ex - Group Photo

很牛的题

设\(A_0=A_{n+1}=INF\),那么对于每个\(B_i\)有\(B_i>\min(A_{i-1},A_i)\),所以考虑设\(C_i\)表示\(min(A_{i-1},A_i)\),那么有\(B_i>C_i\),显然,若我们将\(C\)从小到大排序,\(B\)也从小到大排序,那么肯定是一一对应的(即满足\(B_i>C_i\)时合法)

考虑\(C_i=A_i\)时,说明若我们将\(A\)也从小到大排序后挨个放入序列中,有\(A_i\)比\(A_{i-1}\)先放入序列中,所以设\(dp[i][j]\)表示前\(i\)个\(A\)放成了\(j\)个连通块

首先若我们放了了\(i\)个\(A\),形成了\(j\)个连通块,那么一定有\(i+j\)个\(C\)被确定并且一定是前\(i+j\)个\(C\),那么前\(i+j\)个\(C\)就一一对应了前\(i+j\)个\(B\),所以考虑转移:

将第\(A_{i+1}\)放在每个块的首/尾,\(dp[i][j]\times2\times j\rightarrow dp[i+1][j](A_{i+1}<B_{i+j+1})\)

用\(A_{i+1}\)链接两个连通块,\(dp[i][j]\times (j-1)\rightarrow dp[i+1][j-1]\),因为此时\(A_{i+1}\)两边的\(A\)都比\(A_{i+1}\)先加入,所以\(A_{i+1}\)不会产生新的\(C\)

\(A_{i+1}\)单独新建一个块,\(dp[i][j]\times(j+1)\rightarrow dp[i+1][j+1](A_{i+1}<B_{i+j+1}\&\&A_{i+1}<B_{i+j+2})\),因为\(B\)排了序,所以\(B_{i+j+1}<B_{i+j+2}\),所以只需要判断\(A_{i+1}<B_{i+j+1}\)

#include<bits/stdc++.h>
using namespace std;
const int N=5e3+5,MOD=998244353;
int dp[N][N],n,a[N],b[N];
void add(int &x,int y){ x+=y; (x>=MOD)&&(x-=MOD); (x<0)&&(x+=MOD); }
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=1;i<=n+1;++i) scanf("%d",&b[i]);
dp[0][0]=1,sort(a+1,a+n+1),sort(b+1,b+n+2);
for(int i=0;i<n;++i){
for(int j=0;j<=i&&i+j<=n+1;++j){
if(a[i+1]<b[i+j+1]) add(dp[i+1][j],dp[i][j]*2ll%MOD*j%MOD);
if(j>1) add(dp[i+1][j-1],1ll*dp[i][j]*(j-1)%MOD);
if(a[i+1]<b[i+j+1]) add(dp[i+1][j+1],1ll*dp[i][j]*(j+1)%MOD);
}
}
printf("%d",dp[n][1]);
return 0;
}

[abc313 h/ex] Group Photo的更多相关文章

  1. PAT A1109 Group Photo (25 分)——排序

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  2. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  3. PAT 1109 Group Photo[仿真][难]

    1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...

  4. 1109 Group Photo (25分)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  5. A1109. Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  6. 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  7. 1109. Group Photo (25)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  8. PAT 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  9. PAT_A1109#Group Photo

    Source: PAT A1109 Group Photo (25 分) Description: Formation is very important when taking a group ph ...

  10. PAT甲级——A1109 Group Photo【25】

    Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...

随机推荐

  1. 理解和使用Oracle 日志分析工具-LogMiner

      分类专栏: oracle日志   原文地址:https://blog.csdn.net/wishfly/article/details/660747 Oracle LogMiner 是Oracle ...

  2. 【QT】Linux下安装QT开发环境

    Linux下安装QT开发环境 零.下载安装包 从以下网址可以下载QT的所有版本: https://download.qt.io/ 这次演示安装5.9.0版本,因为是长期支持的版本,所以进入到archi ...

  3. SQLite 爬坑记录之无法设置自增约束

    场景重现 SQLite在设置自增约束(Autoincrement)的时候出现无法设置的情况: 问题原因 ... 解决办法 ...

  4. LinkedBlockingQueue的put方法底层源码

    一.LinkedBlockingQueue的put方法底层源码 LinkedBlockingQueue 的 put 方法是其核心方法之一,用于将元素插入队列.如果队列已满,调用 put 方法的线程会被 ...

  5. Vue的前端项目开发环境搭建

    一.本机window端:安装Node.js,其实质性功能相当于,java的maven https://nodejs.org/en/download/ 二.本机window端:检查Node.js的版本 ...

  6. Windows执行bat脚本乱码

    解决方案: 将脚本编码设置为ANSI

  7. Web前端入门第 41 问:神奇的 transform 改变元素形状,matrix3d 矩阵变换算法演示

    CSS transform 属性中文翻译过来是 变换,始终觉得翻译差那么一点意思.它可以用来改变元素形状,比如旋转.缩放.移动.倾斜等,就是它可以把元素各种拿捏~ transform 特性是在不改变自 ...

  8. 89.4K star!这个开源LLM应用开发平台,让你轻松构建AI工作流!

    嗨,大家好,我是小华同学,关注我们获得"最新.最全.最优质"开源项目和高效工作学习方法 Dify 是一款开源的 LLM 应用开发平台,通过直观的可视化界面整合 AI 工作流.RAG ...

  9. 一些软件、jar包下载链接、方法

    目录 jar包下载 dbutils C3P0 软件下载 TeamViewer 远程桌面 EV录屏 SublimeText 编辑器 feiQ 通信 文件共享 jdk 8u171 下载 jar包下载 db ...

  10. java中的虚函数

    一.Java中的虚函数 普通函数就是虚函数(同等于C语言中virtual关键词修饰的方法) 虚函数的存在是为了多态 C++中普通成员函数加上virtual关键字就成为虚函数 Java中其实没有虚函数的 ...