201274: [AtCoder]ARC127 E - Priority Queue

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $800$ points

Problem Statement

Given is a sequence of $A+B$ integers $(X_1,X_2,\cdots,X_{A+B})$, which contains exactly $A$ ones and exactly $B$ twos.

Snuke has a set $s$, which is initially empty. He is going to do $A+B$ operations. The $i$-th operation is as follows.

  • If $X_i=1$: Choose an integer $v$ such that $1 \leq v \leq A$ and add it to $s$. Here, $v$ must not be an integer that was chosen in a previous operation.

  • If $X_i=2$: Delete from $s$ the element with the largest value. The input guarantees that $s$ is not empty just before this operation.

How many sets are there that can be the final state of $s$? Find the count modulo $998244353$.

Constraints

  • $1 \leq A \leq 5000$
  • $0 \leq B \leq A-1$
  • $1 \leq X_i \leq 2$
  • $X_i=1$ for exactly $A$ indices $i$.
  • $X_i=2$ for exactly $B$ indices $i$.
  • $s$ will not be empty just before an operation with $X_i=2$.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$A$ $B$
$X_1$ $X_2$ $\cdots$ $X_{A+B}$

Output

Print the answer modulo $998244353$.


Sample Input 1

3 1
1 1 2 1

Sample Output 1

2

There are two possible final states of $s$: $s=\{1,2\},\{1,3\}$.

For example, the following sequence of operations results in $s=\{1,3\}$.

  • $i=1$: Add $2$ to $s$.
  • $i=2$: Add $1$ to $s$.
  • $i=3$: Delete $2$ from $s$.
  • $i=4$: Add $3$ to $s$.

Sample Input 2

20 6
1 1 1 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 1 1

Sample Output 2

5507

Input

题意翻译

给定一个长度为 $a+b$ 的序列 $x$,并且刚好有 $a$ 个 1,$b$ 个 2。 有一个集合 $s$,初始是空的,将会做 $a+b$ 次操作,第 $i$ 次操作如下: 1. $x_i=1$,选择一个数 $v\in[1,a]$,并把这个数加入到集合中,这里 $v$ 必须是之前没有选择过的数 2. $x_i=2$,将集合中最大的数删掉 问最后 $s$ 能有几种状态。 - $1\le a,b\le 5000$

加入题单

上一题 下一题 算法标签: