hdoj 5592 ZYB's Premutation 【线段树插空】
来源:互联网 发布:sql server 2008下载 编辑:程序博客网 时间:2024/06/06 10:04
ZYB's Premutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 31 Accepted Submission(s): 8
Problem Description
restore the premutation.
Pair
Input
In the first line there is the number of testcases T.
For each teatcase:
In the first line there is one numberN .
In the next line there areN numbers Ai ,describe the number of the reverse logs of each prefix,
The input is correct.
1≤T≤5 ,1≤N≤50000
For each teatcase:
In the first line there is one number
In the next line there are
The input is correct.
Output
For each testcase,print the ans.
Sample Input
130 1 2
Sample Output
3 1 2
题意:给定1-n某一个全排列的前缀区间逆序对个数,让你还原这个序列。
思路:线段树插空,逆向跑一遍就ok了。
AC代码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (50000+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;int a[MAXN], b[MAXN];struct Tree{ int l, r, len; int sum;};Tree tree[MAXN<<2];void PushUp(int o){ tree[o].sum = tree[ll].sum + tree[rr].sum;}void Build(int o, int l, int r){ tree[o].l = l; tree[o].r = r; tree[o].len = r - l + 1; tree[o].sum = 1; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson); PushUp(o);}int Query(int o, int v){ if(tree[o].l == tree[o].r) return tree[o].l; int mid = (tree[o].l + tree[o].r) >> 1; if(tree[ll].sum >= v) return Query(ll, v); else return Query(rr, v-tree[ll].sum);}void Update(int o, int P){ if(tree[o].l == tree[o].r) { tree[o].sum = 0; return ; } int mid = (tree[o].l + tree[o].r) >> 1; if(P <= mid) Update(ll, P); else Update(rr, P); PushUp(o);}int main(){ int t; Ri(t); W(t) { int n; Ri(n); for(int i = 1; i <= n; i++) Ri(a[i]); a[0] = 0; Build(1, 1, n); for(int i = n; i >= 1; i--) { int num = a[i] - a[i-1]; int P = Query(1, num+1); //Pi(P); Update(1, P); b[i] = n-P+1; } for(int i = 1; i <= n; i++) { if(i > 1) printf(" "); printf("%d", b[i]); } printf("\n"); } return 0;}
0 0
- hdoj 5592 ZYB's Premutation 【线段树插空】
- HDU 5592 ZYB's Premutation 线段树
- hdu 5592 ZYB's Premutation (线段树)
- HDU 5592(ZYB's Premutation-线段树)
- hdu 5592 ZYB's Premutation 线段树
- hdu-5592 ZYB's Premutation(线段树)
- HDOJ 5592 ZYB's Premutation(逆序对变题:树状数组+二分||线段树)
- hdu 5592 ZYB's Premutation(线段树求逆序数)
- hdu 5592 ZYB's Premutation(线段树)
- hdu 5592 ZYB's Premutation 经典线段树
- HDU.5592 ZYB's Premutation(线段树求第k大)
- HDU 5592ZYB's Premutation (线段树求第K大)
- HDU 5592 ZYB's Premutation
- HDU 5592 ZYB's Premutation
- HDU 5592 ZYB's Premutation
- HDU 5592 ZYB's Premutation
- hdu 5592 ZYB's Premutation
- hdu 5592 ZYB's Premutation(线段树查询第k大的数)
- android5.0自带兼容控件__Toolbar
- hdoj 5591 ZYB's Game 【水题】
- Hdoj.5407 CRB and Candies【数论。。。】 2015/12/05
- sax/dom/jdom/dom4j的区别?
- 黑马程序员——C语言基础---指针
- hdoj 5592 ZYB's Premutation 【线段树插空】
- 从DeepLearnToolbox-master看CNN
- Android生成keystore的两种方式
- 【Leetcode】Spiral Matrix II
- 同时安装Java 1.7和1.8
- 【tmp】LR(0)
- 02_Java 重写(Override)与重载(Overload)
- 深入了解scanf() getchar()和gets()等函数之间的区别
- mysql配置优化(windows下my.ini) 参数设置