[Codeforces 501D] - Misha and Permutations Summation
题意是给你两个长度为$n$的排列,他们分别是$n$的第$a$个和第$b$个全排列。输出$n$的第$\left(a+b \right)\textrm{mod} \, n!$个全排列。
一种很容易的想法是直接把$a$和$b$求出来,然后计算$\left(a+b \right)\textrm{mod} \, n!$的值并直接求对应的排列,但是由于$n$的范围$\left(n\leq200000\right)$直接求值显然不可行。
因此,考虑全排列的康托展开(Cantor expansion) 任意一种排列在全排列中对应的序号为$$\sum_{i=1}^{n}{a}_{i}\times i!$$
于是,将输入的两个排列分别写成这种形式,然后遍历$n$相加,由于结果需要对$n!$取模,因此从最低位开始逐项将$a_i$加到$a_i+1$上去,最后将最高位的$a_n$模掉$n$即可。
之后,只要拟用康托展开即可求出对应的排列。
在实现过程中,由于需要维护"当前还没有使用过的第k大的数",因此可以用树状数组BIT维护。恢复排列时用树状数组+二分即可。
复杂度$\mathcal{O}({n\log}^{2}n )$
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define GETNUM(num) scanf("%d",&num)
#define IT_PT(BEG,END,TYPE,REG) copy(BEG,END,ostream_iterator<TYPE>(cout,REG))
#define CLR(ARR,NUM) memset(ARR,NUM,sizeof(ARR))
#define faster_io() ios_base::sync_with_stdio(false)
using namespace std;
const int MAXN = 200010;
int la[MAXN], lb[MAXN], a[MAXN], b[MAXN], f[MAXN], s[MAXN];
typedef int bit_type;
const int bit_maxn = MAXN;
int n;
int ff[MAXN];
bit_type tree[bit_maxn]; int lowbit(int x)
{
return x & (-x);
} void add(int x, int d)
{
x++;
while(x <= n) {
tree[x] += d;
x += lowbit(x);
}
} bit_type sum(int x)
{
x++;
bit_type ans = 0;
while(x) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
cin >> n;
for(int i = 0; i < n; i++)
GETNUM(la[i]);
for(int i = 0; i < n; i++)
GETNUM(lb[i]);
CLR(tree, 0);
for(int i = 0; i < n; i++) {
add(i, 1);
}
for(int i = 0; i < n; i++) {
a[i] = sum(la[i] - 1);
add(la[i], -1);
}
CLR(tree, 0);
for(int i = 0; i < n; i++) {
add(i, 1);
}
for(int i = 0; i < n; i++) {
b[i] = sum(lb[i] - 1);
add(lb[i], -1);
s[i] = a[i] + b[i];
} CLR(tree, 0);
for(int i = 0; i < n; i++) {
add(i, 1);
ff[i] = 1;
}
for(int i = n - 1; i > 0; i--) {
s[i - 1] += s[i] / (n - i);
s[i] %= (n - i);
}
s[0] %= n;
int rr = n - 1;
for(int i = 0; i < n; i++) {
int r = rr;
int l = 0;
while(l < r) {
int mid = l + (r - l + 1) / 2;
int t = sum(mid - 1);
if(t <= s[i]) l = mid;
else r = mid - 1;
}
add(l, -1);
ff[l] = 0;
if(!i) {
printf("%d", l);
} else {
printf(" %d", l);
}
while(!ff[rr]) {
rr--;
}
}
return 0;
}
[Codeforces 501D] - Misha and Permutations Summation的更多相关文章
- Misha and Permutations Summation
A - Misha and Permutations Summation 首先这个 mod n! 因为数量级上的差别最多只会对康拓展开的第一项起作用所以这个题并不需要把 ord (p) 和 ord ( ...
- 【codeforces 501D】Misha and Permutations Summation
[题目链接]:http://codeforces.com/problemset/problem/501/D [题意] 给你两个排列; 求出它们的字典序num1和num2; 然后让你求出第(num1+n ...
- Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组
题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, ...
- Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树
思路:很裸的康拓展开.. 我的平衡树居然跑的比树状数组+二分还慢.. #include<bits/stdc++.h> #define LL long long #define fi fir ...
- CF501D Misha and Permutations Summation(康托展开)
将一个排列映射到一个数的方法就叫做康托展开.它的具体做法是这样的,对于一个给定的排列{ai}(i=1,2,3...n),对于每个ai求有多少个aj,使得j>i且ai>aj,简单来说就是求a ...
- CodeForces 501B Misha and Changing Handles(STL map)
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...
- codeforces 501C. Misha and Forest 解题报告
题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...
- Codeforces Round #337 Alphabet Permutations
E. Alphabet Permutations time limit per test: 1 second memory limit per test: 512 megabytes input: ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- EntityFramework - Migrations
EntityFramework - Migrations 對項目進行EF的數據庫升級操作.分爲開發環境與部署環境.上的操作總結. 引用: Command說明https://coding.abel.n ...
- JSP的优势与劣势浅析
本文简单介绍了JSP技术,并对JSP的优势与劣势进行了简单的分析.JSP页面由HTML代码和嵌入其中的Java代码所组成. JSP(JavaServer Pages)是由Sun Microsystem ...
- BZOJ 1032 祖玛
Description 这是一个流行在Jsoi的游戏,名称为祖玛.精致细腻的背景,外加神秘的印加音乐衬托,彷佛置身在古老的国度里面,进行一个神秘的游戏——这就是著名的祖玛游戏.祖玛游戏的主角是一只石青 ...
- 【Java】String,StringBuffer与StringBuilder的区别??
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...
- Color the Fence
Codeforces Round #202 (Div. 2) B:http://codeforces.com/problemset/problem/349/B 题意:给你一些颜料,然后你可以用这些颜料 ...
- org.springframework.web.context.ContextLoaderListener
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...
- 转:Mysql读写分离实现的三种方式
1 程序修改mysql操作类可以参考PHP实现的Mysql读写分离,阿权开始的本项目,以php程序解决此需求.优点:直接和数据库通信,简单快捷的读写分离和随机的方式实现的负载均衡,权限独立分配缺点:自 ...
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(一)
最近,一直在写PPC的模拟器和汇编器,也在做设计.所以重新看了看<鸟哥的Linux私房菜>,还是有好多命令不太熟悉.就打算写几篇blog记下来. 1. nl [-bnw] filename ...
- HDOJ 2055 An easy problem
Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, - f(Z) = 26, f(z) = -26; Giv ...
- 动态规划——A 最大子段和
A - 最大子段和 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...