uva 11525 - Permutation(线段树)
来源:互联网 发布:剑灵 职业 知乎 编辑:程序博客网 时间:2024/06/09 16:19
题目链接:uva 11525 - Permutation
题目大意:给定n和k,n给定的方式为k个si,根据公式计算出n,求一个由1~k组成的长度为k的序列的第n个排序
解题思路:根据公式的性质,等于对于每个位置找当前状态下第si小的数。线段树子节点均为1,维护和,查询时传入参数查找即可。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 50005;#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)+1)struct Node { int l, r, val; void set (int l, int r, int val) { this->l = l; this->r = r; this->val = val; }}node[maxn * 4];void pushup(int u) { node[u].set(node[lson(u)].l, node[rson(u)].r, node[lson(u)].val + node[rson(u)].val);}void build_segTree (int u, int l, int r) { if (l == r) { node[u].set(l, r, 1); return ; } int mid = (l + r) / 2; build_segTree(lson(u), l, mid); build_segTree(rson(u), mid + 1, r); pushup(u);}int query_segTree (int u, int val) { if (node[u].l == node[u].r && val == 1) return node[u].l; if (val > node[lson(u)].val) return query_segTree(rson(u), val - node[lson(u)].val); else return query_segTree(lson(u), val);}void insert_segTree (int u, int x, int val) { if (node[u].l == x && node[u].r == x) { node[u].val += val; return ; } int mid = (node[u].l + node[u].r) / 2; if (x <= mid) insert_segTree(lson(u), x, val); else insert_segTree(rson(u), x, val); pushup(u);}int main () { int cas; scanf("%d", &cas); while (cas--) { int n, x; scanf("%d", &n); build_segTree(1, 1, n); for (int i = 1; i <= n; i++) { scanf("%d", &x); x = query_segTree(1, x+1); insert_segTree(1, x, -1); printf("%d%c", x, i == n ? '\n' : ' '); } } return 0;}
1 0
- uva 11525 - Permutation(线段树)
- UVa 11525 Permutation (线段树)
- 【UVA】11525-Permutation(线段树水题)
- Permutation +uva+线段树+点修改
- UVa 11525 - Permutation (线段树 树状数组 康托展开式)
- UVA 11525 Permutation(线段树第K大数字问题)
- Uva-11525-Permutation
- UVA 11525 Permutation
- uva 11525 Permutation
- UVA - 11525 Permutation
- Uva 11525 Permutation
- uva - 11525 Permutation
- UVA 11525Permutation
- uva 11525 Permutation
- UVA11525 - Permutation(线段树)
- Codeforces452F Permutation -- 线段树 + Hash
- [线段树 哈希] Codeforces452F. Permutation
- UVA 11525 - Permutation(树状数组)
- 从零开始学android<Menu菜单组件.三十.>
- 请求发送者与接收者解耦——命令模式
- tomcat配置https
- 透明div层
- 快速入门C++ 09(静态属性和静态方法)
- uva 11525 - Permutation(线段树)
- 用链表实现队列
- 串口结构
- NSLocale的一些方法
- 排序算法——选择排序
- 高权重网站打造计划之USER需求原理
- iMatrix平台中组织结构树标签(Ztree实现)用法
- 黑马程序员_反射
- POJ2679 SPFA求负环