[atARC114F]Permutation Division
由于是排列,即任意两个数字都各不相同,因此字典序最大的$q_{i}$就是将每一段的第一个数从大到小排序
接下来,考虑第一个元素,也就是每一段开头的最大值,分类讨论:
1.当$p_{1}\le k$时,取$1,2,...,k$为每一段开头是唯一一种可以使$q_{i}$以$k$为开头的方案(证明略)
2.当$p_{1}>k$时,假设这$k$个块的开头依次为$a_{1},a_{2},...,a_{k}$(其中$a_{1}=1$),将其按照开头的数字从大到小排序后,依次为$b_{1},b_{2},...,b_{k}$
考虑$a_{i}$和$b_{i}$的最长公共前缀,假设为$l$(即$\forall 1\le i\le l,a_{i}=b_{i}$且$a_{l+1}\ne b_{l+1}$),根据$b$是$a$排序得到,那么$l$还可以用另一种方式来描述:最大的$l$满足$p_{a_{1}}>p_{a_{2}}>...>p_{a_{l}}>\max_{i=l+1}^{k}p_{a_{i}}$
更进一步的,注意下面这两个性质:
1.$q_{i}$的字典序是随着$k$的减小而单调不增的,即在同样的情况下,我们希望$k$小(当$k$缩小时,只需要将任意两段合并,$q_{i}$字典序显然不增)
2.取$k=1$时即$q=p$,根据性质1即可得$q\ge p$,那么$q_{i}$最小的必要条件是其与$p_{i}$的最长公共前缀最长
继续前面的思路,不难发现$a_{l+1}-1$其实就是这一组$p_{i}$和$q_{i}$的最长公共前缀长度,而如果已经(暴力枚举)确定了$l$、$a_{l}$以及$a_{l+1}$,那么问题即变为:
选择一个长度为$l$且以1为开头、$a_{l}$为结尾的递减序列,并对$p_{a_{l+1}},p_{a_{l+1}+1},...,p_{n}$这个序列划分为$k-l$段,要求在每一段开头都小于$p_{a_{l}}$的基础上最小化字典序
当$a_{l}$确定时,我们是希望$l$尽量大的,而这个$l$也就是最长下降子序列,$o(n\log n)$预处理出来
更进一步的,在$l$和$a_{l}$确定后,我们希望$a_{l+1}$尽量大,考虑如何判定一个$a_{l+1}$是否合法,只需要满足:1.$p_{a_{l+1}}<p_{a_{l}}$;2.在$p_{a_{l+1}},p_{a_{l+1}+1},...,p_{n}$中存在$k-l$个数比$p_{a_{l}}$小
更具体的,所谓$a_{l+1}$其实就是在$p_{n}$往前,第$k-l$个比$p_{a_{l}}$小的数,那么其之后(包括其自身)恰好存在$k-l$个比$p_{a_{l}}$小的数,必然以这些数为开头,即确定了划分的方案
关于如何找到$a_{l+1}$,将1到$n$每一个数在$p_{i}$中的位置依次插入,插入时末尾第$k-l$小即为答案,用线段树维护并在其上二分即可
找到$a_{l+1}$后,实际上这最后的$k-l$个数也可以看作$p_{a_{l+1}},p_{a_{l+1}+1},...,p_{n}$中最小的$k-l$个数,那么显然我们仍然可以在$a_{l+1}$最大的情况下找到最大的$l$即可
总复杂度即$o(n\log n)$,可以通过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define L (k<<1)
5 #define R (L+1)
6 #define mid (l+r>>1)
7 int n,k,a[N],pos[N],dp[N],nex[N],b[N],f[N<<2];
8 void update1(int k,int l,int r,int x,int y){
9 if (l==r){
10 f[k]=y;
11 return;
12 }
13 if (x<=mid)update1(L,l,mid,x,y);
14 else update1(R,mid+1,r,x,y);
15 f[k]=max(f[L],f[R]);
16 }
17 int query1(int k,int l,int r,int x,int y){
18 if ((l>y)||(x>r))return -0x3f3f3f3f;
19 if ((x<=l)&&(r<=y))return f[k];
20 return max(query1(L,l,mid,x,y),query1(R,mid+1,r,x,y));
21 }
22 void update2(int k,int l,int r,int x){
23 f[k]++;
24 if (l==r)return;
25 if (x<=mid)update2(L,l,mid,x);
26 else update2(R,mid+1,r,x);
27 }
28 int query2(int k,int l,int r,int x){
29 if (l==r)return l;
30 if (x<=f[R])return query2(R,mid+1,r,x);
31 return query2(L,l,mid,x-f[R]);
32 }
33 int main(){
34 scanf("%d%d",&n,&k);
35 for(int i=1;i<=n;i++)scanf("%d",&a[i]);
36 for(int i=1;i<=n;i++)pos[a[i]]=i;
37 if (a[1]<=k){
38 for(int i=k;i;i--){
39 printf("%d ",i);
40 for(int j=pos[i]+1;(j<=n)&&(a[j]>k);j++)printf("%d ",a[j]);
41 }
42 return 0;
43 }
44 memset(f,-0x3f,sizeof(f));
45 for(int i=1;i<=n;i++){
46 dp[i]=query1(1,1,n,a[i]+1,n)+1;
47 if (a[i]<=a[1])dp[i]=max(dp[i],1);
48 update1(1,1,n,a[i],dp[i]);
49 }
50 for(int i=1;i<=n;i++)
51 if (dp[i]>=k){
52 for(int j=1;j<=n;j++)printf("%d ",a[j]);
53 return 0;
54 }
55 memset(f,0,sizeof(f));
56 for(int i=1;i<=n;i++){
57 nex[pos[i]]=query2(1,1,n,k-dp[pos[i]]);
58 if (nex[pos[i]]<pos[i])nex[pos[i]]=0;
59 update2(1,1,n,pos[i]);
60 }
61 for(int i=1;i<=n;i++)nex[0]=max(nex[0],nex[i]);
62 for(int i=1;i<=n;i++)
63 if (nex[i]==nex[0])dp[0]=max(dp[0],dp[i]);
64 for(int i=1;i<nex[0];i++)printf("%d ",a[i]);
65 for(int i=nex[0];i<=n;i++)b[i-nex[0]]=a[i];
66 sort(b,b+n-nex[0]+1);
67 for(int i=k-dp[0]-1;i>=0;i--){
68 printf("%d ",b[i]);
69 for(int j=pos[b[i]]+1;(j<=n)&&(a[j]>b[k-dp[0]-1]);j++)printf("%d ",a[j]);
70 }
71 }
[atARC114F]Permutation Division的更多相关文章
- python from __future__ import division
1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 关于分工的思考 (Thoughts on Division of Labor)
Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...
随机推荐
- spoj2 prime1 (区间筛)
给定t组询问,每组询问包括一个l和r,要求\([l,r]\)的素数有哪些 其中\(t \le 10,1 \le l \le r \le 1000000000 , r-l \le 100000\) Qw ...
- Sentinel-Go 源码系列(一)|开篇
大家好呀,打算写一个 Go 语言组件源码分析系列,一是为了能学习下 Go 语言,看下别人是怎么写 Go 的,二是也掌握一个组件. 本次选择了 Sentinel-Go,一是对 Java 版本的 Sent ...
- javascript-jquery选择器
jquery选择器用来获得jquery对象 我们用一个实例来演示jquery与原生的区别 <div id="title">123</div>原生获得元素的方 ...
- 对比7种分布式事务方案,还是偏爱阿里开源的Seata,真香!(原理+实战)
前言 这是<Spring Cloud 进阶>专栏的第六篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得了? 阿里面 ...
- linux:桌面切换
永久更改 字符模式:multi-user.target 图形模式:graphical.target systemctl get-default #查看默认模式 systemctl set-defaul ...
- Java:修饰符小记
Java:修饰符小记 对 Java 中的 修饰符,做一个微不足道的小小小小记 Java 语言提供了很多修饰符,大概分为两类: 访问权限修饰符 非访问权限修饰符 访问权限修饰符 修饰符 说明 publi ...
- HTTP请求如何带参
这两天正好作一份API的接口文档,关于HTTP request如何传递参数不是很清楚,这里转载了他人的文档,让我明白了很多.. http://tomfish88.iteye.com/category/ ...
- CSP2021 翻车记
DAY - INF 日常模拟赛被吊打,不知道为啥总是出一些小问题导致正解gg,成绩的话也就是中游吧,不过方差不小 DAY - 2 感冒了,头疼得很,签到题甚至也签到失败了,烦得很 DAY -1 全真体 ...
- 从四个方向分析我们可以从linux学到什么
我们真正关心的是自身可以从这个生态圈中获得些什么?说得更直白一点就是,我们可以从linux系统上面学到点什么,它对我们个人的成长和发展有哪些积极的因素.个人觉得,完全可以通过下面四个维度并结合自己的兴 ...
- shell调用另一个脚本的三种方式fork/exec/source
exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. bash shell的命令分为两 ...