题目简述:有一个全排列,一直每个前缀区间的逆序对数,还原这个排列。

fi记录逆序对数,pi记录该位置数值,则k=fi-f(i-1)表示前i-1个数比pi大的数的个数,那么只要在剩余元素求出按大小顺序第i-k个数字即可。

线段树+二分搜索,线段树bit[i]记录i的在剩余元素的排名顺序。

 /*******************************

 Date    : 2015-12-06 19:49:59
Author : WQJ (1225234825@qq.com)
Link : http://www.cnblogs.com/a1225234/
Name : ********************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
int a[+];
int bit[+];
int ans[+];
int n;
int lowbit(int i)
{
return i&-i;
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=lowbit(i);
}
return s;
}
void add(int i,int a)
{
while(i<=n)
{
bit[i]+=a;
i+=lowbit(i);
}
}
int main()
{
freopen("in.txt","r",stdin);
int i,j;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
a[]=;
memset(bit,,sizeof(bit));
for(i=;i<=n;i++) add(i,); /*用树状数组记录i的大小排名*/
for(i=n;i>=;i--)
{
int temp=a[i]-a[i-];
temp=i-temp; //排在第temp的数
int l=,r=n,mid;
int k=r;
while(r>=l)
{
mid=(r+l)/;
if(sum(mid)>=temp) {k=mid;r=mid-;}
else l=mid+;
}
ans[i]=k;
add(ans[i],-);
}
for(i=;i<=n;i++)
printf("%d%c",ans[i],i==n?'\n':' ');
}
return ;
}

BC 65 ZYB's Premutation (线段树+二分搜索)的更多相关文章

  1. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

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

  2. 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​​大的数的个数.我们考虑倒着做,当我们处理 ...

  3. L3-002. 堆栈【主席树 or 线段树 or 分块】

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道"堆栈"是一种"先 ...

  4. ZYB's Premutation(有逆序数输出原序列,线段树)

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

  5. 线段树 - ZYB's Premutation

    ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ...

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

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

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

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

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

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

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

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

随机推荐

  1. Qt带进度条的启动界面(继承QSplashScreen,然后使用定时器)

    通过继承QSplashScreen类,得到CMySplashScreen类,然后在CMySplashScreen中定义QProgressBar变量,该变量以CMySplashScreen为父类,这样就 ...

  2. PostgreSQL与MySQL比较(转)

    Mysql 使用太广泛了,以至于我不得不将一些应用从mysql 迁移到postgresql, 很多开源软件都是以Mysql 作为数据库标准,并且以Mysql 作为抽象基础的,但是具体使用过程中,发现M ...

  3. java.lang.OutOfMemoryError: unable to create new native thread(转)

    解决 - java.lang.OutOfMemoryError: unable to create new native thread 工作中碰到过这个问题好几次了,觉得有必要总结一下,所以有了这篇文 ...

  4. The Most Wanted Letter

    The Most Wanted Letter You are given a text, which contains different english letters and punctuatio ...

  5. c# aes 加密解密

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. HashMap Java Doc

    原文 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneab ...

  7. 如何在自定义Listener(监听器)中使用Spring容器管理的bean

    正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...

  8. python高级编程之访问超类中的方法:super()

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #超类01 #它是一个内建类型,用于访问属于某个对象超类特性 pri ...

  9. eclipse里添加类似myeclipse打开当前操作目录

    1.开打eclipse ide,依次run->external tools->external tools configuration 2.在Program下,new一个自己定义的prog ...

  10. Mysql 分别按月, 日为组group,进行统计排序order

    在数据库中我们经经常使用sql语句对表进行简单的统计排序,对于日期字段.我们能够简单的对其进行order. 对于复杂一点的能够按日期中的年.月,日分别进行group,order. 按年份进行group ...