全排列 UVA 11525 Permutation
题意:训练指南P248
分析:逆向考虑,比如一个全排列:7345261,它也可以表示成题目中的形式,第一个数字7是由6 * (7 - 1)得到的,第二个数字3有2 * (7 - 2)得到,所以只要树状数组单点修改二分(找最远的因为有些位置是0)查询当前第s[i] + 1的数字(在BIT中指前p项和为s[i] + 1)。
#include <bits/stdc++.h>
using namespace std; const int N = 1e5 + 5;
int s[N];
struct BIT {
int c[N], sz;
void init(int num) {
memset (c, 0, sizeof (c));
sz = num;
}
void updata(int i, int x) {
while (i <= sz) {
c[i] += x; i += i & -i;
}
}
int query(int i) {
int ret = 0;
while (i > 0) {
ret += c[i]; i -= i & -i;
}
return ret;
}
int bsearch(int l, int r, int kth) {
while (l < r) {
int mid = l + r >> 1;
if (query (mid) < kth) l = mid + 1;
else r = mid;
}
return r;
}
}bit; int main(void) {
int T; scanf ("%d", &T);
while (T--) {
int k; scanf ("%d", &k);
bit.init (k);
for (int i=1; i<=k; ++i) {
scanf ("%d", &s[i]);
bit.updata (i, 1);
}
for (int i=1; i<=k; ++i) {
int p = bit.bsearch (1, k, s[i] + 1);
if (i > 1) printf (" ");
printf ("%d", p);
bit.updata (p, -1);
}
puts ("");
} return 0;
}
全排列 UVA 11525 Permutation的更多相关文章
- UVA 11525 Permutation(树状数组)
题目意思是说 给你一个数k 然后有k个si 问你1--k 的第n个全排列是多少 注意是 1 2 3...k的全排列 不是si的 N= 由观察得知(k-i)!就是k-i个数字的全排列种数 ...
- UVA 11525 Permutation (树状数组+YY)
题意:给你k个数Si,然后给你一个等式 H= ∑ Si ∗ (K − i)! (i=(1->k)且0 ≤ Si ≤ K − i). 叫你求出第H个全排列 其实这是一个康托展开:X=a[n ...
- UVA 11525 Permutation ——(线段树,脑筋急转弯)
只要注意到对于譬如:S1*(k-1)! 因为后面k-1个数字的全排列个数刚好是(k-1)!,那么第一个数字就是没有取过的数字的第(S1+1)个即可.取走这个数字以后这个数字就不能再用了,依次类推即可得 ...
- Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)
Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...
- uva 11525(线段树)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 全排列 递归方法(permutation原理
https://blog.csdn.net/axiqia/article/details/50967863 原博客 (一)递归的全排列算法 (A.B.C.D)的全排列为 1.A后面跟(B.C.D)的 ...
- [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- UVA 11922 Permutation Transformer(平衡二叉树)
Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...
- uva 11922 Permutation Transforme/splay tree
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...
随机推荐
- eclipse快捷键Alt + / 失效
最近电脑上的Eclipse没有了自动提示功能,也不是全部不提示,大多数情况下按下"alt+/"键还会产生提示,但是当我在java项目中邪main方法和syso的时候,"a ...
- [Android] adb shell dumpsys的使用
reference to :http://blog.csdn.net/g19920917/article/details/38032413 有两种方法可以查看service list: 1. adb ...
- object实现小老鼠交互
直接使用 <p style="text-align: center; "> <object type="application/x-shockwave- ...
- Angularjs调用公共方法与共享数据
这个问题场景是在使用ionic开发页面的过程中发现,多个页面对应的多个controller如何去调用公共方法,比如给ionic引入了toast插件,如何将这个插件的调用变成公共方法或者设置成工具类,因 ...
- AFNetworking(AFN)总结
AFNetworking(AFN) ----主要做下载上传之用 //他是干啥的?发送请求,主要做下载上传之用 (苹果自带有获取服务器数据的方法NSURLConnection send,AFNetwor ...
- Java -- File
@.getPath().getAbsolutePath().getCanonicalPath()区别 原文:http://blog.csdn.net/wh_19910525/article/detai ...
- 与你相遇好幸运,CentOS 7 x86_64使用Yum安装PostgreSQL
访问http://yum.pgrpms.org/reporpms/repoview/letter_p.group.html,下载并安装和当前系统对应的rpm文件. wget https://downl ...
- Delphi的属性Property
参考:http://www.cnblogs.com/edisonfeng/archive/2012/05/22/2513727.html 一.基本属性 TOnUserInfoShow = proced ...
- MVC部分视图(Partial View)
分部视图,也就是整体视图的一部分.单个视图页面展示在整体页面之上,使用步骤如下 1.创建视图数据也就是viewmodel public class FooterViewModel { public s ...
- Juery Ajax语法
$.ajax({ url: "/ForgetCard/ForgetLogin",//方法路径URL data: { strUser: $("#textUser" ...