[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 ...
随机推荐
- S_型文法到q_型文法再到LL(1)型文法演进笔记
title: S_型文法到q_型文法再到LL(1)型文法演进笔记 date: 2020-08-23 S_型文法到q_型文法再到LL(1)型文法演进笔记 S_型文法(简单的确定性文法) 每个产生式的右部 ...
- caffe运行错误 target_blobs.blobs_size()与 source_layer.blobs_size() 不一致
解决方法参考:http://blog.csdn.net/zhangla1220/article/details/50697352 感谢博主!!! 最新下载的caffe代码,运行mnist,训练时可以正 ...
- 为什么下一个十年的主战场在 Serverless?
作者 | 不瞋 阿里云 Serverless 负责人 "唯有超越,才能让我们走下去." 这是不瞋在阿里的第十年.从 2010 年加入阿里云,不瞋参与了阿里云飞天分布式系统的研发,历 ...
- 题解 [JSOI2011]柠檬
题目传送门 题目大意 给出一个区间,每个点都有一个颜色,把这个区间分为许多块,每一块的权值为 \(\max\{s\times t^2\}\) ,其中 \(s\) 为某种颜色,\(t\) 为该颜色在该块 ...
- Java(34)IO流之字符流
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228453.html 博客主页:https://www.cnblogs.com/testero ...
- windows右键菜单自动打包发布nuget,没有CI/CD一样方便!
构建现代的 .Net 应用离不开 Nuget 的支持,而快速打包 Nuget 成了提高生产率的有效方法.没有CI/CD?来试试使用windows右键菜单吧 先看右键效果图 有时候我们可能没有CI/CD ...
- python中的信号通信 blinker
信号: 信号是一种通知或者说通信的方式,信号分为发送方和接收方.发送方发送一中信号,接收方收到信号的进程会跳入信号处理函数,执行完后再跳回原来的位置继续执行.常见的linux中的信号,通过键盘输入Ct ...
- 脚本注入2(post)
终于写到非get类型的注入了. 不过,我懒得在这里搞代码审计了:留到存储型XSS原型的时候再来分析源码吧. 这次以Less-15为例. 框里随便输点东西,submit,抓包,发现包出现了一些改变: 同 ...
- WEB安全指南
说明:本文是Mozilla Web应用部署文档,对运维或者后端开发团队的部署行为进行指导.该部署安全规范内容充实,对于部署有很大意义.同时也涉及到了许多web前端应用安全的基本知识,如CSP, TOK ...
- 关于takin-data,你想知道的都在这里(一)启动命令篇
通过docker部署体验takin的小伙伴都应该知道,在安装部署手册中有提到:在启动surge-deploy任务前,需要将启动命令中的ip参数替换为docker容器所在宿主机的ip,很多小伙伴都在这里 ...