[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 ...
随机推荐
- ASP.NET Core 学习笔记 第三篇 依赖注入框架的使用
前言 首先感谢小可爱门的支持,写了这个系列的第二篇后,得到了好多人的鼓励,也更加坚定我把这个系列写完的决心,也能更好的督促自己的学习,分享自己的学习成果.还记得上篇文章中最后提及到,假如服务越来越多怎 ...
- Linux 下 xargs 命令
xargs 常常被大家忽略的一个命令,对它的一些用法很多人可能不熟悉,其实它是一个功能强大的命令,特别是在结合管道进行批量处理方面 语法 xargs 语法格式如下 xargs [OPTION]... ...
- SONiC架构分析
目录 系统架构 设计原则 核心组件 SWSS 容器 syncd 容器 网络应用容器 内部通信模型 SubscriberStateTable NotificationProducer/Consumer ...
- Conversion Tools(转换工具)
转换工具 1.Excel # Process: Excel 转表 arcpy.ExcelToTable_conversion("", 输出表, "") # Pr ...
- 题解 P6454 麻将 加强版
题目传送门 题目大意 不想写了,直接看题面吧. 思路 被这个题搞自闭了,因为读错题目想一个非常恶心的东西想了一场考试,然后就删代码,然后就被骂了.哎. 需要注意的是:我们只能选一次雀头 于是,不难看出 ...
- Golang通脉之接口
接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类型. interface是 ...
- UVA-1498 Activation
UVA-1498 DP应该是肯定的,设 f [ i ] [ j ] 表示现在对中共有 i 人,Tomato在第 j 个,出现所求情况的概率,我们可以很(简单的)艰难的列出下列方程: f[i][1] = ...
- Qt信号与槽传递自定义数据类型——两种解决方法
信号与槽作为qt中的核心机制,在qt应用开发中经常会用的,但是原生的信号与槽连接传参,只支持基本的数据类型,比如char,int, float,double. 如果想要在信号与槽之间传递自定义参数,比 ...
- best-time-to-buy-and-sell-stock-ii leetcode C++
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
- hdu 5083 Instruction (稍比较复杂的模拟题)
题意: 二进制指令转汇编指令,汇编指令转二进制指令. 思路: 额,条理分好,想全,思维不能乱. 代码: int findyu(char yu[50],char c){ int l=strlen(yu) ...