Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 175    Accepted Submission(s): 74

Problem Description
ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to 
restore the premutation.

Pair (i,j)(i<j) is considered as a reverse log if Ai>Aj is matched.

 
Input
In the first line there is the number of testcases T.

For each teatcase:

In the first line there is one number N.

In the next line there are N numbers Ai,describe the number of the reverse logs of each prefix,

The input is correct.

1≤T≤5,1≤N≤50000

 
Output
For each testcase,print the ans.
 
Sample Input
1
3
0 1 2
 
Sample Output
3 1 2
 
Source

思路:容易想到k = (a[i] - a[i - 1] + 1)就是原序列第i个数的左边比其大的个数,可以从右往左依次计算

用线段树实现:序列中每个存在的位置相当于有一个1,不存在位0,从中找出第k个1所在的位置,找到后删除该数相当于置0

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <queue>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <iostream>
  11. #define lson l, m, rt << 1
  12. #define rson m + 1, r, rt << 1|1
  13. using namespace std;
  14. const int INF = 0x3f3f3f3f;
  15. typedef long long ll;
  16.  
  17. const int N = ;
  18. int num[N << ], a[N], ans[N];
  19. void up(int rt) { num[rt] = num[rt << ] + num[rt << |]; }
  20. void build(int l, int r, int rt)
  21. {
  22. if(l == r) {
  23. num[rt] = ;
  24. return ;
  25. }
  26. int m = (l + r) >> ;
  27. build(lson);
  28. build(rson);
  29. up(rt);
  30. }
  31. void update(int l, int r, int rt, int p)
  32. {
  33. if(l == p && r == p) {
  34. num[rt]--;
  35. return ;
  36. }
  37. int m = (l + r) >> ;
  38. if(p <= m) update(lson, p);
  39. else update(rson, p);
  40. up(rt);
  41. }
  42. int pos;
  43. void get(int l, int r, int rt, int x)
  44. {
  45. if(l == r) {
  46. pos = l;
  47. return;
  48. }
  49. int m = (l + r) >> ;
  50. if(num[rt << ] < x) get(rson, x - num[rt << ]);
  51. else get(lson, x);
  52. }
  53. int main()
  54. {
  55. int _; scanf("%d", &_);
  56. while(_ --)
  57. {
  58. int n; scanf("%d", &n);
  59. for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
  60. build(, n, );
  61. int c = n;
  62. for(int i = n; i >= ; --i)
  63. {
  64. int d = c - (a[i] - a[i - ]);
  65. get(, n, , d);
  66. update(, n, , pos);
  67. // cout << pos << endl;
  68. ans[i] = pos;
  69. c--;
  70. }
  71. get(, n, , ); ans[] = pos;
  72. for(int i = ; i < n; ++i) printf("%d ", ans[i]);
  73. printf("%d\n", ans[n]);
  74. }
  75. return ;
  76. }

Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树的更多相关文章

  1. hdu 5592 ZYB's Premutation(线段树优化)

    设f_if​i​​是第ii个前缀的逆序对数,p_ip​i​​是第ii个位置上的数,则f_i-f_{i-1}f​i​​−f​i−1​​是ii前面比p_ip​i​​大的数的个数.我们考虑倒着做,当我们处理 ...

  2. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  3. HDU 5592——ZYB's Premutation——————【线段树单点更新、单点查询】

    ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. HDU 5592 ZYB's Premutation

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  5. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. HDU 5592 ZYB's Premutation(树状数组+二分)

    题意:给一个排列的每个前缀区间的逆序对数,让还原 原序列. 思路:考虑逆序对的意思,对于k = f[i] - f[i -1],就表示在第i个位置前面有k个比当前位置大的数,那么也就是:除了i后面的数字 ...

  7. HDU - 5592 ZYB's Premutation (权值线段树)

    题意:给出序列前k项中的逆序对数,构造出这个序列. 分析:使用权值线段树来确定序列元素. 逆序对的数量肯定是递增的,从最后一个元素开始逆向统计,则\(a[i] - a[i-1]\)即位置i之前比位置i ...

  8. BC 65 ZYB's Premutation (线段树+二分搜索)

    题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列. fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第 ...

  9. HDU 5592 ZYB's Game 【树状数组】+【二分】

    <题目链接> 题目大意: 给你一个由1~n,n个数组成的序列,给出他们每个的前缀逆序数,现在要求输出这个序列. 解题分析: 由前缀逆序数很容易能够得到每个数的逆序数.假设当前数是i,它前面 ...

随机推荐

  1. html与js传json值给php

    //一段js代码 var data = {}, act = [], list = []; $('.set').find('input, textarea').each(function() { act ...

  2. October 3rd 2016 Week 41st Monday

    Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. Sitting in the darkness and wa ...

  3. Codeforces Round #304 C(Div. 2)(模拟)

    题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...

  4. java 缩略图

    http://www.cnblogs.com/digdeep/p/4829471.html http://www.jb51.net/article/57648.htm http://blog.csdn ...

  5. CentOS FTP基于虚拟用户的配置

    详细可以看:http://www.linuxidc.com/Linux/2013-12/94242.htm 所谓虚拟用户就是没有使用真实的帐户,只是通过映射到真实帐户和设置权限的目的.虚拟用户不能登录 ...

  6. 【PHP数组的使用】

    PHP数组使用关键字array标识,数组内的元素可以是任意类型,而且可以不是同一种类型,这和c.java不同. 遍历数组的方法可以使用foreach,也可以使用for循环 可以使用print_r或者v ...

  7. [LeetCode] Ugly Number

    Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...

  8. postgresql设置默认的search_path

    -- Use this to show the current search_path -- Should return: "$user",public SHOW search_p ...

  9. C# Qrcode生成二维码支持中文,带图片,带文字 2015-01-22 15:11 616人阅读 评论(1) 收藏

    1.下载Qrcode库源码,下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 2.打开源码时,部分类库 ...

  10. 1-02 启动和停止Sql Sever的服务

    启动Sql  Sever服务的三种方式 1:后台启动服务. 2:Sql Sever配置管理员启动服务. 3:在运行窗口中使用命令启动和停止服务: 启动:net start mssqlsever. 停止 ...