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. WPF --- 非Button自定义控件实现点击功能

    引言 今天在做一个设置文件夹路径的功能,就是一个文本框,加个按钮,点击按钮,弹出 FolderBrowserDialog 再选择文件夹路径,简单做法,可以直接 StackPanel 横向放置一个 Te ...

  2. Python+Flask接口实现简单的ToKen功能

    话不多说,上代码 from flask import request, jsonify from functools import wraps class TokenRequired: @classm ...

  3. Docker部署cas

    一.首先安装cas镜像 1.拉取cas docker镜像 docker pull apereo/cas 2.启动容器: docker run  --name cas -p 8443:8443 -p 8 ...

  4. 文盘Rust——子命令提示,提高用户体验

    上次我们聊到 CLI 的领域交互模式.在领域交互模式中,可能存在多层次的子命令.在使用过程中如果全评记忆的话,命令少还好,多了真心记不住.频繁 --help 也是个很麻烦的事情.如果每次按 'tab' ...

  5. 拟合优度R2较低怎么办

    拟合优度R2较低怎么办 (1)回归分为解释型回归和预测型回归. 预测型回归一般才会更看重2. 解释型回归更多的关注模型整体显著性以及自变量的统计显著性和经济意义显著 性即可. (2)可以对模型进行调整 ...

  6. pandas处理大数据题目的操作

    1.用法:DataFrame.drop(labels=None, axis=0, index=None, columns=None, inplace=False) 2.参数说明: labels:要删除 ...

  7. Redis系列之——主从复制原理与优化、缓存的使用和优化

    @ 目录 一 什么是主从复制 1.1 原理 1.2 主库是否要开启持久化 1.3 辅助配置(主从数据一致性配置) 二 复制的 配置 2.1 slave 命令 2.2 配置文件 四 故障处理 五 复制常 ...

  8. Linux系列教程——Linux文件编辑、Linux用户管理

    @ 目录 1 Linux基本权限 1.权限基本概述 1.什么是权限? 2.为什么要有权限? 3.权限与用户之间的关系? 4.权限中的rwx分别代表什么含义? 2.权限设置示例 1.为什么要设定权限,我 ...

  9. answer answerdev/answer:latest 开源问答平台

    freedidi.com/10294.html   https://youtu.be/A2GUgvPlTBE?si=jdhqXL1WttLrLgiQ     docker依赖 yum install ...

  10. SP3377

    题目简化和分析: 前言:这题目背景真奇怪. 我们可以将每种关系,看成一条边,如果出现奇数边环就不满足. 例如:\(a,b\) 异性 \(a,c\) 异性 \(b,c\)异性 这种情况是不满足的. 相当 ...