ZYB's Premutation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 638    Accepted Submission(s): 302

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. 1
  2. 3
  3. 0 1 2
 
Sample Output
  1. 3 1 2
 

题目链接:点击打开链接

能够想到a[i] - a[i - 1]为i前比a[i]大的数的个数, 从后向前遍历, 通过二分与树状数组确定i应当填入的位置.

AC代码:

  1. #include "iostream"
  2. #include "cstdio"
  3. #include "cstring"
  4. #include "algorithm"
  5. #include "queue"
  6. #include "stack"
  7. #include "cmath"
  8. #include "utility"
  9. #include "map"
  10. #include "set"
  11. #include "vector"
  12. #include "list"
  13. #include "string"
  14. using namespace std;
  15. typedef long long ll;
  16. const int MOD = 1e9 + 7;
  17. const int INF = 0x3f3f3f3f;
  18. const int MAXN = 5e4 + 5;
  19. int n, a[MAXN], c[MAXN], ans[MAXN];
  20. int lowbit(int x)
  21. {
  22. return x & (-x);
  23. }
  24. void update(int x, int y)
  25. {
  26. while(x <= n) {
  27. c[x] += y;
  28. x += lowbit(x);
  29. }
  30. }
  31. int get_sum(int x)
  32. {
  33. int sum = 0;
  34. while(x > 0) {
  35. sum += c[x];
  36. x -= lowbit(x);
  37. }
  38. return sum;
  39. }
  40. int binary_search(int x)
  41. {
  42. int l = 1, r = n, m;
  43. while(l <= r) {
  44. m = (l + r) >> 1;
  45. if(get_sum(m) >= x) r = m - 1;
  46. else l = m + 1;
  47. }
  48. return l;
  49. }
  50. int main(int argc, char const *argv[])
  51. {
  52. int t;
  53. scanf("%d", &t);
  54. while(t--) {
  55. memset(c, 0, sizeof(c));
  56. memset(ans, 0, sizeof(ans));
  57. scanf("%d", &n);
  58. for(int i = 1; i <= n; ++i)
  59. scanf("%d", &a[i]);
  60. for(int i = n; i >= 1; --i)
  61. a[i] -= a[i - 1];
  62. for(int i = 1; i <= n; ++i)
  63. update(i, 1);
  64. for(int i = n; i >= 1; --i) {
  65. int pos = i - a[i] - 1;
  66. ans[i] = binary_search(pos + 1);
  67. update(ans[i], -1);
  68. }
  69. for(int i = 1; i < n; ++i)
  70. printf("%d ", ans[i]);
  71. printf("%d\n", ans[n]);
  72. }
  73. return 0;
  74. }

BestCoder Round #65 HDOJ5592 ZYB&#39;s Premutation(树状数组+二分)的更多相关文章

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

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

  2. BestCoder Round #65 (ZYB's Premutation)

    ZYB's Premutation Accepts: 220 Submissions: 983 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  3. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  4. BestCoder Round #65 (ZYB's Game)

    ZYB's Game Accepts: 672 Submissions: 1207 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...

  5. BestCoder Round #65 (ZYB's Biology)

    ZYB's Biology Accepts: 848 Submissions: 1199 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 13 ...

  6. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

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

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

  8. Codeforces Round #381 (Div. 2) D dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组

    D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...

随机推荐

  1. linux Cenos-7.0下安装jdk搭建环境变量

    搭建linux系统的java环境本人目前所知有两种方式: 第一种是从oracle官网下载linux系统的jdk版本: 第二种是使用yum命令的方式下载安装,咱们今天讲的是第一种方式. 一.工具准备: ...

  2. HDU 4917 Permutation 拓扑排序的计数

    题意: 一个有n个数的排列,给你一些位置上数字的大小关系.求合法的排列有多少种. 思路: 数字的大小关系可以看做是一条有向边,这样以每个位置当点,就可以把整个排列当做一张有向图.而且题目保证有解,所以 ...

  3. Oracle11g密码忘记处理方法

    c:\>sqlplus /nolog sql>connect / as sysdba sql>alter user 用户名 identified by 密码;(注意在这里输入的密码是 ...

  4. USART

    串口通信是一种设备间非常常用的串行通行方式,其简单便捷,大部分电子设备都支持. 一.物理层 常用RS-232标准,主要规定了信号的用途.通信接口以及信号的电平标准.  “DB9接口”之间通过串口信号线 ...

  5. 1193 Eason

    Eason Acceteped : 57   Submit : 129 Time Limit : 1000 MS   Memory Limit : 65536 KB Description 题目描述 ...

  6. 洛谷 P1407 工资

    P1407 工资 题目描述 有一家世界级大企业,他们经过调查,发现了一个奇特的现象,竟然在自己的公司里,有超过一半的雇员,他们的工资完全相同! 公布了这项调查结果后,众多老板对于这一现象很感兴趣,他们 ...

  7. BZOJ 1002 FJOI 2007 轮状病毒 暴力+找规律+高精度

    题目大意: 思路:基尔霍夫矩阵求生成树个数,不会. 可是能够暴力打表.(我才不会说我调试force调试了20分钟... CODE(force.cc): #include <cstdio> ...

  8. java基础 this keyword!

    为了程序的可读性,通常将一个类中的表示同一属性的变量进行统一的命名.可是这样做又会导致成员变量与局部变量名字冲突导致无法訪问成员变量.为了解决问题,java中引入了this这个keyword!所以th ...

  9. CODE ---代码助手 (保存代码、搜代码、生成网页、自由界面)

    四大功能 1  保存代码 2  搜索代码 3  生成网页 4  自由界面 www.gudianxiaoshuo.com

  10. HDU 4431 Mahjong 模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=4431 不能说是水题了,具体实现还是很恶心的...几乎优化到哭但是DFS(还加了几个剪枝)还是不行...搜索一直T ...