hdu 5592 BestCoder Round #65(树状数组)
来源:互联网 发布:织梦cms模板下载 编辑:程序博客网 时间:2024/06/03 19:14
题意:
ZYB有一个排列P,但他只记得P中每个前缀区间的逆序对数,现在他要求你还原这个排列.(i,j)(i<j)被称为一对逆序对当且仅当Ai>Aj
输入描述
第一行一个整数T表示数据组数。接下来每组数据:第一行一个正整数N,描述排列的长度.第二行N个正整数Ai,描述前缀区间[1,i]的逆序对数.数据保证合法.1≤T≤5,1≤N≤50000
输出描述
T行每行N个整数表示答案的排列.
输入样例
130 1 2
输出样例
3 1 2
思路:
a[i]表示[1,i]的逆序对,所以a[i] - a[i-1]便是i前面比第i个数大的,所以a[i]-a[i-1]+1便是第i个数在[1,i]中第几大。
所以用树桩数组全部初始化为1,然后二分+树状数组找出第k大的数然后把其赋值为0即可。
#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long ll;#define N 100050int a[N];int ans[N];int tree[N],dis[N];int n;void add(int x,int dx){ while(x <= n) { tree[x] += dx; x += x&(-x); }}int query(int x){ int sum = 0; while(x) { sum += tree[x]; x -= x&(-x); } return sum;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1; i <= n; i++) add(i,1); for(int i = 1; i <= n; i++) scanf("%d",a+i); for(int i = n; i >= 1; i--) { dis[i] = a[i]-a[i-1]+1; int l = 1,r = n; int tt; while(l < r) { int mid = (l+r)/2; if(query(mid) < dis[i]) l = mid+1; else if(query(mid) > dis[i]) r = mid-1; else { tt = r; r -= 1; } } ans[i] = r; add(l,-1); } for(int i= 1; i <= n; i++) printf("%d%c",n+1-ans[i],(i==n)? '\n':' '); }}
0 0
- hdu 5592 BestCoder Round #65(树状数组)
- HDU 5748 BestCoder Round #84 Bellovin (LIS)(树状数组)
- HDU 5808 BestCoder Round #86 Price List Strike Back (树状数组)
- BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
- HDU 5273 Dylans loves sequence——BestCoder Round #45(DP or 树状数组)
- HDU 5997 BestCoder Round #90 1003 rausen loves cakes 线段树 树状数组
- hdu 5592 BestCoder Round #65 1003
- hdu 5591 BestCoder Round #65(博弈)
- BestCoder Round #16 Revenge of Segment Tree (树状数组)
- bestcoder:树状数组+lca
- BestCoder Round #65 B C D || HDU 5591 5592 5593
- hdu 5147 Sequence II BestCoder #23 —树状数组
- BestCoder 2nd Anniversary B hdu 5719 树状数组
- BestCoder Round #65 HDU 5592 ZYB's Premutation (数据结构查询第K大)
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
- BestCoder Round #16 1002(hdu 5087)
- 20140929 【 树状数组 】 BestCoder Round #11 (Div. 2) Argestes and Sequence
- BestCoder Round #52 (div.2)(hdu 5417,hdu 5418)
- UGUI中image不能使用旧版Animation
- C++学习知识点总结(1)
- NOIP2015 day2t3 运输计划
- What is JSON?
- 杭电-1034Candy Sharing Game(模拟)
- hdu 5592 BestCoder Round #65(树状数组)
- LINUX打印
- Arduino 平台与C语言程序设计-week1-Arduino Environment-Lesson3
- Best Time to Buy and Sell Stock with Cooldown
- Nginx 配置SSL访问
- 编译原理 LR(0)项目集规范族的构造 LR(0)分析表+分析语句 详解分析+代码
- splay区间翻转及删除区间,平移区间
- 实例Block传值
- XMLDLL操作说明文档(三)