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

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll; const int N = ;
int num[N << ], a[N], ans[N];
void up(int rt) { num[rt] = num[rt << ] + num[rt << |]; }
void build(int l, int r, int rt)
{
if(l == r) {
num[rt] = ;
return ;
}
int m = (l + r) >> ;
build(lson);
build(rson);
up(rt);
}
void update(int l, int r, int rt, int p)
{
if(l == p && r == p) {
num[rt]--;
return ;
}
int m = (l + r) >> ;
if(p <= m) update(lson, p);
else update(rson, p);
up(rt);
}
int pos;
void get(int l, int r, int rt, int x)
{
if(l == r) {
pos = l;
return;
}
int m = (l + r) >> ;
if(num[rt << ] < x) get(rson, x - num[rt << ]);
else get(lson, x);
}
int main()
{
int _; scanf("%d", &_);
while(_ --)
{
int n; scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
build(, n, );
int c = n;
for(int i = n; i >= ; --i)
{
int d = c - (a[i] - a[i - ]);
get(, n, , d);
update(, n, , pos);
// cout << pos << endl;
ans[i] = pos;
c--;
}
get(, n, , ); ans[] = pos;
for(int i = ; i < n; ++i) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}
return ;
}

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. 【网络】VPN

    VPN: 来自百度百科 虚拟专用网络的功能是:在公用网络上建立专用网络,进行加密通讯.在企业网络中有广泛应用.VPN网关通过对数据包的加密和数据包目标地址的转换实现远程访问.VPN有多种分类方式,主要 ...

  2. php原型模式的研究

    <?php class Sea{} class EarthSea extends Sea{} class MarsSea extends Sea{} class Plains{} class E ...

  3. C++静态代码分析工具对比cppCheck与PreFast

    具体内容参看文件<CppCheck和PreFast对Cplusplus代码静态分析测试.zip> C++测试源代码main.cpp #define NULL 0 #include < ...

  4. iOS 控制屏幕旋转

    在你想支持横竖屏的viewController里面重写两个方法: 1 2 3 4 5 6 7 8 9 10 11 // 支持设备自动旋转 - (BOOL)shouldAutorotate {      ...

  5. 避免产生僵尸进程的N种方法(zombie process)

    http://blog.csdn.net/duyiwuer2009/article/details/7964795 认识僵尸进程 1.如果父进程先退出 子进程自动被 init 进程收养,不会产生僵尸进 ...

  6. OpenCv,EmguCv及.net之间的互动(The Interaction of OpenCv, EmguCv AND .net)

    http://www.cnblogs.com/xrwang/archive/2010/01/26/TheInteractionOfOpenCv-EmguCvANDDotNet.html 前言 在.ne ...

  7. Asp.net窄屏页面 手机端新闻列表

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchNotice.a ...

  8. 【JAVA网络流之浏览器与服务器模拟】

    一.模拟服务器获取浏览器请求http信息 代码: package p06.TCPTransferImitateServer.p01.ImitateServer; import java.io.IOEx ...

  9. 理解理解python中的'*','*args','**','**kwargs'

    http://blog.csdn.net/callinglove/article/details/45483097 讲了一大堆, 我也是用来理解类继承当中的参数行为的. =============== ...

  10. JVM_Bind:8080 的解决办法【131031】

    出错情况:运行 Tomcat 时报错 含义:8080 位置显示的端口被其他进程占用 解决方法: 方法1: 开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID ...