Problem Statement

You are given an integer sequence $A = (A_1, \dots, A_N)$ of length $N$.

Find the number, modulo $998244353$, of permutations $P = (P_1, \dots, P_N)$ of $(1, 2, \dots, N)$ such that:

  • there exist exactly $K$ integers $i$ between $1$ and $(N-1)$ (inclusive) such that $A_{P_i} \lt A_{P_{i + 1}}$.

Constraints

  • $2 \leq N \leq 5000$
  • $0 \leq K \leq N - 1$
  • $1 \leq A_i \leq N \, (1 \leq i \leq N)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $K$
$A_1$ $\ldots$ $A_N$

Output

Print the answer.


Sample Input 1

4 2
1 1 2 2

Sample Output 1

4

Four permutations satisfy the condition: $P = (1, 3, 2, 4), (1, 4, 2, 3), (2, 3, 1, 4), (2, 4, 1, 3)$.


Sample Input 2

10 3
3 1 4 1 5 9 2 6 5 3

Sample Output 2

697112

考虑 $dp$

先提一个问题,如果保证给出来的是一个排列,有多少种可能?

考虑从小到大插入数字,这样能保证插入的时候这个数无论在已插入的数的哪里,都是最大的。定义 \(dp_{i,j}\) 为插入了前 \(i\) 大的数,此时有 \(j\) 个顺序对的方案数。

这个数有 \(j+1\) 中方式使答案不变,因为如果他插到了顺序对之间,或者查到了序列的末尾,顺序对数量不变。同理,有 \(i-j\) 种方式使顺序对数量加一。

那么排列的情况我们会了,不是排列怎么办?如果这个数前面有 \(c\) 个和他一样的,如果他插到了一个和他一样的数的后面,顺序对数量也不会增加。而插入的地方两边一定不是顺序对(至多相等)。所以方程变为 \(dp_{i,j}=dp_{i-1,j}\times (j+1+c)+dp_{i-1,j-1}\times (i-j-c)\)

#include<bits/stdc++.h>
using namespace std;
const int N=5005,P=998244353;
int n,k,dp[N][N],c[N],ans,inv[N],iv[N],a[N];//dp[i][j]表示按照从小到大顺序插入,已经插入前i大的数,有j个顺序对
int main()
{
scanf("%d%d",&n,&k);
for(int i=1,x;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
inv[1]=iv[1]=iv[0]=1;
for(int i=1;i<=n;i++)
{
inv[i]=1LL*(P-P/i)*inv[P%i]%P;
iv[i]=iv[i-1]*inv[i]%P;
}
dp[1][0]=1;
for(int i=2,cnt=0;i<=n;i++)
{
if(a[i]==a[i-1])
++cnt;
else
cnt=0;
for(int j=0;j<=k;j++)//
{
dp[i][j]=1LL*dp[i-1][j]*(j+1+cnt)%P;
if(j)
(dp[i][j]+=1LL*dp[i-1][j-1]*(i-j-cnt)%P)%=P;
}
}
ans=dp[n][k];
// printf("%d",ans);
// for(int i=1;i<=n;i++)
// ans=1LL*ans*iv[c[i]]%P;
printf("%d",ans);
return 0; }

[ABC267G] Increasing K Times的更多相关文章

  1. ABC267G Increasing K Times 题解

    做这道题,很有感悟,发篇文. 先给数列从小到大排个序. 接下来设 \(f_{i,j}\) 表示前 \(i\) 个数的排列形成 \(j\) 个上坡的方案数. 接下来考虑转移,分为插入第 \(i\) 个数 ...

  2. spark Using MLLib in Scala/Java/Python

    Using MLLib in ScalaFollowing code snippets can be executed in spark-shell. Binary ClassificationThe ...

  3. Understanding the Bias-Variance Tradeoff

    Understanding the Bias-Variance Tradeoff When we discuss prediction models, prediction errors can be ...

  4. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

  5. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  6. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  7. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  9. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  10. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

随机推荐

  1. 5、Mybatis之获取参数值

    5.1.创建新module 5.1.1.右击SSM文件夹,创建新module 5.1.2.选择maven 5.1.3.配置module名称和路径 5.1.4.module初始状态 5.1.5.复制打包 ...

  2. Tarjan基础用法

    \(\operatorname{Tarjan}\) 基础用法 目录 \(\operatorname{Tarjan}\) 基础用法 \(\operatorname{Tarjan}\) 求最近公共祖先 前 ...

  3. legend的用法

    常规使用legend的方法 1. 自动检测 设置title的label标签,随后使用不带参数的legend函数也会自动显示(但不限于一种方式设置图形的label,只要设置了图形的label,使用leg ...

  4. vue3 甘特图(一):选择与初始化甘特图

    vue3 甘特图(一) 1.功能使用背景: 甘特图是一种项目管理工具,以图形直观的方式显示项目的时间轴和任务计划,为了可扩展和定制相关任务的开发,故此选择dhtmlx-gantt 2.vue3 初始化 ...

  5. Java实践项目 - 用户登录

    Smiling & Weeping ----以花祈愿,祝你平安 当用户输入用户名和密码将数据提交给数据库进行查询,如果存在对应的用户名和密码,则表示登陆成功,登录成功之后跳转到系统的主页就是i ...

  6. webapi开发框架实践

    项目链接以及目录结构 liuzhixin405/efcore-template (github.com) 这是一个纯webapi的开发框架. 1.支持的orm有efcore6.dapper,可以灵活切 ...

  7. C#学习笔记--变量类型的转换

    变量类型的转化: 转换原则 同类型的大的可以装小的,小类型的装大的就需要强制转换. 隐式转换: 同种类型的转换: //有符号 long-->int-->short-->sbyte l ...

  8. Kubernetes集群管理面板的安装及使用

    Kubernetes集群管理面板的安装及使用 1.前言 若海的腾讯云Lighthouse组建跨地域Kubernetes集群,让我成功体验到了Kubernetes集群诸多优点,但是非技术出生的我,长时间 ...

  9. React-Admin后台管理模板|react18+arco+zustand后台解决方案

    基于react18.x+vite4+arco-design自研中后台管理系统解决方案ReactAdmin. react-vite-admin 基于vite4搭建react18.x后台管理项目.使用了r ...

  10. 概率期望 DP 题解合集

    期望这东西学了一次忘了,再学一次过了两天又不会了.我是鱼. 故写此博客以便加深记忆及日后复习. NOIP 前恶补期望(? 希望有用,RP++() 经典问题 1 某事件发生概率为 \(p\),则该事件首 ...