[洛谷U72177]火星人plus
题目大意:给你一个$1\sim n(n\leqslant 10^5)$的排列,设$a$为它在$1\sim n$的全排列中的排名,求在$1\sim n$的全排列中第$a+m$个排列。
题解:康托展开以及逆康托展开。将原排列转为变进制数,加上$m$,再用转回排列。转回去可以用在树状数组上二分来解决。这里使用了$skip2004$教的两种方法。
卡点:变进制数加法时写错
C++ Code:
#include <cstdio>
#include <iostream>
#include <algorithm>
const int maxn = 1 << 17; int n, L = 1, nn; namespace BIT {
int V[maxn], res;
inline void inc(int p) { for (; p <= L; p += p & -p) ++V[p]; }
inline void dec(int p) { for (; p <= L; p += p & -p) --V[p]; }
inline int sum(int p) { for (res = 0; p; p &= p - 1) res += V[p]; return res; }
inline void fill(int p, int L) { for (int i = 0; i < L; ++i) V[i] = p * (i & -i); }
inline int query(int k) { // 树状数组上二分
// 区间法
static int l, r, mid;
l = 1, r = L;
while (l != r) {
mid = l + r >> 1;
if (V[mid] < k) k -= V[mid], l = mid + 1;
else r = mid;
}
dec(l);
return l;
// 跳点法
static int rt; rt = L;
for (int i = rt; i >>= 1; ) {
if (V[rt - i] < k) k -= V[rt - i];
else rt -= i;
}
dec(rt);
return rt;
}
} long long a[maxn], m;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n >> m;
while (L < n) L <<= 1;
for (int i = 1, t; i <= n; ++i) {
std::cin >> a[i];
t = BIT::sum(a[i]);
BIT::inc(a[i]);
a[i] = a[i] - t - 1;
}
a[n] += m;
for (int i = n; i; --i) {
a[i - 1] += a[i] / (n - i + 1);
a[i] %= n - i + 1;
}
BIT::fill(1, L + 1);
for (int i = 1; i <= n; ++i) std::cout << BIT::query(a[i] + 1) << ' ';
std::cout << '\n';
return 0;
}
[洛谷U72177]火星人plus的更多相关文章
- 洛谷P1088 火星人【STL】【思维】
题目:https://www.luogu.org/problemnew/show/P1088 题意: 给定一个n个数的排列,要求得到这之后的第m个排列. 思路: next_permutation的简单 ...
- 洛谷P1088 火星人 [STL]
题目传送门 火星人 格式难调,题面就不放了. 分析: 这道题目不得不又让人感叹,还是$STL$大法好!!! $C++$的$algorithm$库中自带有$next\_permutation()$和$p ...
- 洛谷——P1088 火星人
P1088 火星人 题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常 ...
- 洛谷P1088——火星人(全排列+数学模拟)
题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数字告诉人类科学 ...
- 洛谷 P1088 火星人
https://www.luogu.org/problemnew/show/P1088 这个题一开始是很蒙的 感觉很麻烦,每次都要交换balabala..... 后来才知道有这么一个神奇的stl 真是 ...
- 洛谷 P1088 火星人 (全排列)
直接调用next_permutation即可,向前的话可以调用prev_permutation #include<cstdio> #include<cctype> #inclu ...
- 洛谷P1088 火星人
//其实就是全排列 //我们从外星人给的那串数字往下搜索 //一直往下拓展m次 //最后输出结果 //虽然看起来很暴力,但是题目上说了m非常小 #include<bits/stdc++.h> ...
- 洛谷试炼场-简单数学问题-P1088 火星人
洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...
- bzoj 1014: 洛谷 P4036: [JSOI2008]火星人
题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...
随机推荐
- java如何判断溢出
public int reverse2(int x) { double ans=0; int flag=1; if(x<0){ flag=-1; } x=x*flag; while(x>0 ...
- rust学习
Rust (github) 1. install (https://rustup.rs/) 2. play on line curl https://sh.rustup.rs -sSf | sh e ...
- CH定理与线性递推
才发觉自己数学差的要死,而且脑子有点浑浑噩噩的,学了一个晚上才学会 如果说的有什么不对的可以在下面嘲讽曲明 以下无特殊说明时,默认方阵定义在实数域上,用\(|A|\)表示\(A\)的行列式 特征值与特 ...
- (LIS)最长上升序列(DP+二分优化)
求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...
- mysql cos() 函数
mysql> ); +--------------------+ | cos(PI()/) | +--------------------+ | 0.5000000000000001 | +-- ...
- 模板 - 部分C++库
__builtin系列 据说是GCC自带的系列,在本地装有 GNU GCC Compiler 的 Codeblocks 和 Codeforces 等平台都可以使用这些.但是没办法从 Codeblock ...
- HTTP/1.0和HTTP/1.1 http2.0的区别,HTTP怎么处理长连接(阿里)
HTTP1.0 HTTP 1.1主要区别 长连接 HTTP 1.0需要使用keep-alive参数来告知服务器端要建立一个长连接,而HTTP1.1默认支持长连接. HTTP是基于TCP/IP协议的,创 ...
- scrapy+splash 爬取京东动态商品
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3159 splash是容器安装的,从docker官网上下载windows下的 ...
- Jmeter5.11安装
jmeter5.11要对应jdk1.8以上版本 1.选择zip后缀进行下载 2.配置环境变量 (1)电脑桌面---->"计算机"图标---->鼠标右键选择"属 ...
- windows环境下安装nginx
(1)下载 官网:http://nginx.org/en/docs/windows.html 下载:http://nginx.org/en/docs/windows.html 点击:nginx/Win ...