链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592

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

思路:对于每个位置i,a[i]-a[i-1]就是它前面比它大的数,这样就能知道它是从1到i中第几大的数了。从第n个位开始找,每找到一个数给它标记掉。在线段树中存没标记的数,用二分查找数的大小。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 struct st
 6 {
 7     int l,r;
 8     int sum;
 9 };
10 st tree[500005];
11 void build(int l,int r,int p)
12 {
13     tree[p].l=l;
14     tree[p].r=r;
15     if (l==r)
16     {
17         tree[p].sum=1;
18         return ;
19     }
20     int tem=(l+r)/2;
21     build(l,tem,p*2);
22     build(tem+1,r,p*2+1);
23     tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
24 }
25 int find(int l,int r,int p)
26 {
27     if (tree[p].l>=l&&tree[p].r<=r) return tree[p].sum;
28     int tem=(tree[p].l+tree[p].r)/2;
29     if (l>tem) return find(l,r,p*2+1);
30     else if (r<=tem) return find(l,r,p*2);
31     return find(l,tem,p*2)+find(tem+1,r,p*2+1);
32 }
33 void un(int x,int p)
34 {
35     if (tree[p].l==tree[p].r)
36     {
37         tree[p].sum=0;
38         return ;
39     }
40     if (tree[p*2].r>=x) un(x,p*2);
41     else un(x,p*2+1);
42     tree[p].sum=tree[p*2].sum+tree[p*2+1].sum;
43 }
44 int main()
45 {
46     int t,n,A[50005],a[50005],v[50005];
47     int i,j,b,c,l,r,s;
48     scanf("%d",&t);
49     while (t--)
50     {
51         scanf("%d",&n);
52         memset(v,0,sizeof(v));
53         for (i=1;i<=n;i++) scanf("%d",&A[i]);
54         build(1,n,1);
55         for (i=n;i>1;i--)
56         {
57             b=i-(A[i]-A[i-1]);
58             l=b;
59             r=n;
60             while (l<=r)
61             {
62                 c=(l+r)/2;
63                 s=find(1,c,1);
64                 if (s==b&&!v[c]) break;
65                 if (s<b) l=c+1;
66                 else r=c-1;
67             }
68             a[i]=c;
69             v[c]=1;
70             un(c,1);
71         }
72         for (i=1;i<=n;i++)
73         {
74             if (!v[i])
75             {
76                 a[1]=i;
77                 break;
78             }
79         }
80         for (i=1;i<n;i++) printf("%d ",a[i]);
81         printf("%d\n",a[i]);
82     }
83 }

转载于:https://www.cnblogs.com/pblr/p/5023380.html

hdu 5592 ZYB's Premutation (线段树+二分查找)相关推荐

  1. HDU - 4614 Vases and Flowers 线段树+二分

    题目链接 思路:线段树维护区间和,当k=1时,询问二分询问[x-(x~n-1)]找到最小位置,复杂度n*logn*logn卡过 #include<stdio.h> #include< ...

  2. 牛客小白月赛28 E-会当凌绝顶,一览众山小 线段树+二分暴力模拟

    牛客小白月赛28 E-会当凌绝顶,一览众山小 线段树+二分暴力模拟 题意 思路 Code 传送门: https://ac.nowcoder.com/acm/contest/16081/E 题意 登山顺 ...

  3. HDU 1166 敌兵布阵(线段树:点更新,区间求和)

    HDU 1166 敌兵布阵(线段树:点更新,区间求和) http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意: 给你n个整数,然后给你多条命令,每条命令如 ...

  4. codeforces 609F Frogs and mosquitoes 线段树+二分+multiset

    http://codeforces.com/problemset/problem/609/F There are n frogs sitting on the coordinate axis Ox. ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. 2019CCPC网络赛 1002 HDU 6703(权值线段树)

    2019CCPC网络赛 1002 HDU 6703(权值线段树) 思路:用权值线段树存题目给的数据后,2操作就是求权值线段树中大于等于k的部分中,靠近左端点的第一个大于r的值(这个求出来的只是原序列中 ...

  7. HDU - 5592 ZYBs Premutation(线段树,逆序对)

    题目链接:点击查看 题目大意:给出 n 个数,分别表示数列 p 前缀 [ 1 , i ] 的逆序对个数,现在要求还原数列 p 题目分析:设 a[ i ] 为前缀 [ 1 , i ] 的逆序对个数,则 ...

  8. HDU 6070 Dirt Ratio(线段树、二分)

    http://acm.hdu.edu.cn/showproblem.php?pid=6070 题解 首先不难看出错误率是单调的,那么我们可以直接二分答案x,某个区间的错误率=区间数的种类cnt/区间长 ...

  9. 【HDU - 5875】Function(线段树,区间第一个小于某个数的数 或 RMQ二分)

    题干: The shorter, the simpler. With this problem, you should be convinced of this truth.        You a ...

最新文章

  1. vue单页面跳页没有数据了_详解刷新页面vuex数据不消失和不跳转页面的解决
  2. 最佳实践: 勿在 Servlet 中实现 SingleThreadModel
  3. 20161104面试题-面试常问问题
  4. java response 输出流_java-springmvc+filter 替换输出流、response、响应内容
  5. 4 WM配置-主数据-定义编码范围
  6. HDU 5533 Dancing Stars on Me( 有趣的计算几何 )
  7. JavaWeb:下载文件
  8. SylixOS 内存管理源代码分析--phyPage.c
  9. JVM调优总结-调优方法
  10. as ssd中文版测试软件,AS SSD Benchmark
  11. ServerStatus 云探针部署
  12. vuecli3.0用scss写响应式网页,封装简单的百分比换算函数
  13. STM32F103_study43_The punctual atoms(STM32 Echo experiment based on serial communication )
  14. 教你如何一键批量删除空间说说
  15. node联合echarts简单实现疫情地图
  16. Kubernetes(k8s)之Volume(卷)
  17. 零基础自学C#——Part4:类的表现形式
  18. 工业交换机与商业交换机区别对比
  19. [学习][Vim]行号的显示与隐藏
  20. 《漫画傅里叶解析》笔记(6)

热门文章

  1. kafka架构组件概念详解:Broker、Topic、Partition、Leader/Follower、Consumer Group、zookeeper
  2. Linux管道用法示例
  3. 使用 Docker 部署 Spring Boot 项目
  4. Java基础--通过反射获取成员方法并使用
  5. Object类toString()和equals()方法剖析
  6. 基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理
  7. python 保存本地乱码_请教大神,如何解决保存后的文件的乱码问题
  8. QML做类似Android圆形头像
  9. Java中用户向系统传递参数的三种基本方式
  10. 如何快速学会嵌入式?