[atAGC054C]Roughly Sorted
考虑对于确定的排列$\{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的更多相关文章
- 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. ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- 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 ...
- 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 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [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 ...
- [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 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
随机推荐
- mysql安装教程,mysql安装配置教程
MySQL的安装教程 一.MYSQL的安装 首先登入官网下载mysql的安装包,官网地址:https://dev.mysql.com/downloads/mysql/ 一般下载这个就好,现在的最新版本 ...
- 11.4.2 LVS—NAT
Virtual Server via NAT(VS-NAT) 用地址翻译实现虚拟服务器。地址转换器有能被外界访问到的合法IP地址,它修改来自专有网络的流出包的地址。外界看起来包是来自地址转换器本身,, ...
- this指向与call,apply,bind
this指向与call,apply,bind ❝ 「this」问题对于每个前端同学来说相信都不陌生,在平时开发中也经常能碰到,有时候因为「this」还踩过不少坑,并且「this」问题在面试题中出现的概 ...
- springMVC上传和下载附件
上传: 导入需要的jar包:Spring MVC类库 + 文件上传下载需要的JAR包,图中A处为文件上传下载需要的JAR包,其余为Spring MVC类库. 构建领域模层:model层和control ...
- Java(7)流程控制语句中的for、while、do while循环
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201543.html 博客主页:https://www.cnblogs.com/testero ...
- Google Style Guides
Google Style Guides Google Style Guides Google 开源项目风格指南 (zh-google-styleguide.readthedocs.io)
- 如何从一台OPC Server访问多个PLC
项目中遇到如下情况: 1. 整条生产线由多个PLC分别控制,但是所有PLC在同一个局域网内.PLC采用西门子的S7-200 Smart 2. 客户希望在操作工站的电脑(跟PLC在同一个局域网内)上提供 ...
- fd定时器--timerfd学习
定时器 可以用系统定时器信号SIGALARM 最近工作需要于是又发现了一个新玩意timerfd配合epoll使用. man 手册看一下 TIMERFD_CREATE(2) Linux Programm ...
- 链表分割 牛客网 程序员面试金典 C++ Python
链表分割 牛客网 程序员面试金典 C++ Python 题目描述 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* p ...
- cf16C Monitor(额,,,,水数学,,)
题意: 一块镜子长宽是a*b.现在要调整(切割)成x:y的比例. 问调整完的最大面积是多少. 思路: 先将x,y弄成最简比例,然后放大到不超过min(a,b)即可. 代码: ll a,b,x,y; l ...