考虑对于确定的排列$\{p_{i}\}$,如何求出其(交换后)会得到的排列——

令$cnt_{x}$为在$i$之前比$x$大的元素个数(其中$p_{i}=x$),显然排列合法当且仅当$cnt_{i}\le k$

注意到每一次交换至多只有初始靠后的元素$cnt_{i}$减小1,因此交换次数至少为$\sum_{i=1}^{n}\max(cnt_{i}-k,0)$

(这里可以简单分析一下交换对$cnt_{i}$的影响,方便理解)

另一方面,取$x=\min_{cnt_{i}>k}i$和$p_{i}=x$,此时必然有$p_{i-1}>p_{i}$(否则即与$x$最小矛盾),因此将$i$和$i-1$交换即可令$cnt_{x}$减小1(且其余$cnt_{i}$不变),重复此过程也即得到了下限

虽然取到下限的方式有很多种,但最终都有$cnt'_{i}=\min(cnt_{i},k)$

同时,对于一组$cnt_{i}\in [0,n-i]$的$\{cnt_{i}\}$,也能唯一确定对应的排列(从大到小依次插入数即可)

换言之,对于原问题来说,即统计有多少组$\{cnt_{i}\}$满足$cnt_{i}\in [0,n-i]$且$cnt'_{i}=\min(cnt_{i},k)$(其中$cnt'_{i}$为给出的排列得到的$cnt_{i}$),显然每一项独立,答案即为$\prod_{1\le i\le n,cnt_{i}=k}(n-i-k+1)$

(这里的构造与原题解略有不同,个人认为更容易理解QAQ)

时间复杂度为$o(n^{2})$或$o(n\log n)$(关于如何计算$cnt_{i}$),可以通过

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 5005
4 #define mod 998244353
5 #define ll long long
6 int n,k,ans,p[N],cnt[N];
7 int main(){
8 scanf("%d%d",&n,&k);
9 for(int i=1;i<=n;i++)scanf("%d",&p[i]);
10 for(int i=1;i<=n;i++)
11 for(int j=1;j<i;j++)
12 if (p[i]<p[j])cnt[p[i]]++;
13 ans=1;
14 for(int i=1;i<=n;i++)
15 if (cnt[i]==k)ans=(ll)ans*(n-i-k+1)%mod;
16 printf("%d\n",ans);
17 return 0;
18 }

[atAGC054C]Roughly Sorted的更多相关文章

  1. Why is processing a sorted array faster than an unsorted array(Stackoverflow)

    What is Branch Prediction? Consider a railroad junction: Image by Mecanismo, via Wikimedia Commons. ...

  2. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  3. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  4. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  5. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  6. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  7. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. ElasticSearch7.X.X-初见-模仿京东搜索的实战

    目录 简介 聊聊Doug Cutting ES&Solr&Lucene ES的安装 安装可视化界面ES head插件 了解ELK 安装Kibana ES核心概念 文档 类型 索引 倒排 ...

  2. JDK源码阅读(3):AbstractStringBuilder、StringBuffer、StringBuilder类阅读笔记

    AbstractStringBuilder abstract class AbstractStringBuilder implements Appendable, CharSequence{ ... ...

  3. JS 实现计算器功能

    括号功能未实现,后续更 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. Java初步学习——2021.09.24每日总结,第三周周五

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天学了将数组传递给方法和方法返回数组,其中传递的是数组的引用. 明天把例子做了,尽量把查找也学习了. 遇到了两个问题: 1 ...

  5. 云原生的弹性 AI 训练系列之三:借助弹性伸缩的 Jupyter Notebook,大幅提高 GPU 利用率

    Jupyter Notebooks 在 Kubernetes 上部署往往需要绑定一张 GPU,而大多数时候 GPU 并没有被使用,因此利用率低下.为了解决这一问题,我们开源了 elastic-jupy ...

  6. [对对子队]会议记录5.21(Scrum Meeting8)

    今天已完成的工作 吴昭邦 ​ 工作内容:调整快进按钮 ​ 相关issue:优化流水线加入物品的动画 ​ 相关签入:feat: 快进图标更换,更改第四关材料位置 朱俊豪 ​ 工作内容:调整场景高度和视角 ...

  7. the Agiles Scrum Meeting 9

    会议时间:2020.4.17 20:00 1.每个人的工作 今天已完成的工作 个人结对项目增量开发组:基本实现个人项目创建.仓库自动分配,修复bug issues: 增量组:准备评测机制,增加仓库自动 ...

  8. 函数指针和qsort函数

    1.函数指针的形式: 函数指针:int (*funcP) (int *a, int *b) 表示定义了一个funcP函数指针,指向了返回值为int类型,参数为int* 和int* 的函数 使用方式: ...

  9. 并发编程从零开始(十二)-Lock与Condition

    并发编程从零开始(十二)-Lock与Condition 8 Lock与Condition 8.1 互斥锁 8.1.1 锁的可重入性 "可重入锁"是指当一个线程调用 object.l ...

  10. (继承)Program2.1

    覆盖和重写的意思是一样?结果是一样的 例如: 1 class Parent: # 定义父类 2 def myMethod(self): 3 print('调用父类方法') 4 5 6 class Ch ...