[ABC267G] Increasing K Times
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的更多相关文章
- ABC267G Increasing K Times 题解
做这道题,很有感悟,发篇文. 先给数列从小到大排个序. 接下来设 \(f_{i,j}\) 表示前 \(i\) 个数的排列形成 \(j\) 个上坡的方案数. 接下来考虑转移,分为插入第 \(i\) 个数 ...
- spark Using MLLib in Scala/Java/Python
Using MLLib in ScalaFollowing code snippets can be executed in spark-shell. Binary ClassificationThe ...
- Understanding the Bias-Variance Tradeoff
Understanding the Bias-Variance Tradeoff When we discuss prediction models, prediction errors can be ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- 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 ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [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 ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
随机推荐
- Geotools实现shape文件的写入
众所周知Geotools作为开源的Java GIS三方库,已经成为GIS服务器端的主流开源库,其功能非常强大,涉及到GIS业务的方方面面,其中就包括GIS数据的读写,今天小编就借助Geotools来实 ...
- 从一些常见的错误聊聊mysql服务端的关键配置
背景 每一年都进行大促前压测,每一次都需要再次关注到一些基础资源的使用问题,订单中心这边数据库比较多,最近频繁报数据库异常,所以对数据库一些配置问题也进行了研究,本文给出一些常见的数据库配置,说明这些 ...
- AI绘画关键词Prompt:分享一些质量比较高的StableDiffusion(SD)关键词网站
今天向大家推荐一些SD(StableDiffusion)高质量的 关键词 网站.这些网站的质量可靠,能为大家在创建 AI 绘画时提供有效的参考.以下是六个推荐的网站,优缺点分析. 有几个质量还算是挺高 ...
- 《CTFshow-Web入门》08. Web 71~80
@ 目录 web71 题解 原理 web72 题解 原理 web73 题解 web74 题解 web75 题解 原理 web76 题解 web77 题解 原理 web78 题解 原理 web79 题解 ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-15-playwright处理浏览器多窗口切换
1.简介 浏览器多窗口的切换问题相比大家不会陌生吧,之前宏哥在java+selenium系列文章中就有介绍过.大致步骤就是:使用selenium进行浏览器的多个窗口切换测试,如果我们打开了多个网页,进 ...
- C#/.NET/.NET Core优秀项目和框架8月简报
前言 公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(公众号每周至少推荐两个优秀的项目和框架当然节假日除外),公众号推文有项目和框架的介绍.功能特点以及部分截图等(打不开或 ...
- springBoot使用注解Aop实现日志模块
我们在日常业务操作中需要记录很多日志,可以在我们需要的方法中对日志进行保存操作,但是对业务代码入侵性大.使用切面针对控制类进行处理灵活度不高,因此我们可以使用自定义注解来针对方法进行日志记录 1.注解 ...
- win如何根据端口号查找并杀死一个线程
查看端口占用 netstat -ano | findstr "端口号" 杀死一个进程 taskkill /pid 进程号 -f
- 给网站添加xml地图索引写法和应用
使用php给网站添加xml地图索引写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- SpringSecurity-前后端分离教程
1.简介 Spring Security 是 Spring 家族中的一个安全管理框架.相比与另外一个安全框架Shiro,它提供了更丰富的功能,社区资源也比Shiro丰富. 一般来说中大型的项目都是使用 ...