ORDERS - Ordering the Soldiers

As you are probably well aware, in Byteland it is always the military officer's main worry to order his soldiers on parade correctly. In Bitland ordering soldiers is not really such a problem. If a platoon consists of n men, all of them have different rank (from 1 - lowest to n - highest) and on parade they should be lined up from left to right in increasing order of rank.

Sounds simple, doesn't it? Well, Msgt Johnny thought the same, until one day he was faced with a new command. He soon discovered that his elite commandos preferred to do the fighting, and leave the thinking to their superiors. So, when at the first rollcall the soldiers lined up in fairly random order it was not because of their lack of discipline, but simply because they couldn't work out how to form a line in correct order of ranks. Msgt Johnny was not at all amused, particularly as he soon found that none of the soldiers even remembered his own rank. Over the years of service every soldier had only learned which of the other soldiers were his superiors. But Msgt Johnny was not a man to give up easily when faced with a true military challenge. After a moment's thought a solution of brilliant simplicity struck him and he issued the following order: "men, starting from the left, one by one, do: (step forward; go left until there is no superior to the left of you; get back in line).". This did indeed get the men sorted in a few minutes. The problem was solved... for the time being.

The next day, the soldiers came in exactly the same order as the day before, and had to be rearranged using the same method. History repeated. After some weeks, Msgt Johnny managed to force each of his soldiers to remember how many men he passed when going left, and thus make the sorting process even faster.

If you know how many positions each man has to walk to the left, can you try to find out what order of ranks the soldiers initially line up in?

Input

The first line of input contains an integer t<=50, the number of test cases. It is followed by t test cases, each consisting of 2 lines. The first line contains a single integer n (1<=n<=200000). The second line contains n space separated integers wi, denoting how far the i-th soldier in line must walk to the left when applying Msgt Johnny's algorithm.

Output

For each test case, output a single line consisting of n space separated integers - the ranks of the soldiers, given from left to right in their initial arrangement.

Example

Input:
2
3
0 1 0
5
0 1 2 0 1 Output:
2 1 3
3 2 1 5 4

思路:线段树;

倒序转化为线段树求第k大的数,复杂度O(\(n*log(n)\));

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<string.h>
#include<map>
typedef long long LL;
using namespace std;
int id[300000];
int tree[4*200005];
int ic[300000];
int ac[300000];
void build(int l,int r,int k);
int ask(int l,int r,int k,int sum);
void update(int k);
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%d",&id[i]);
build(1,n,0);int cn = 0;
int p = n;
for(int i = n-1;i >= 0;i--)
{
ac[cn] = ask(1,n,0,p-id[i]);
p--;
update(ic[ac[cn]]);
cn++;
}
printf("%d",ac[n-1]);
for(int i = n-2;i >= 0;i--)
{
printf(" %d",ac[i]);
}
printf("\n");
}
return 0;
}
void build(int l,int r,int k)
{
if(l == r)
{
tree[k] = 1;
ic[l] = k;
return ;
}
build(l,(l+r)/2,2*k+1);
build((l+r)/2+1,r,2*k+2);
tree[k] = tree[2*k+1] + tree[2*k+2];
}
int ask(int l,int r,int k,int sum)
{
if(tree[k] == sum&&tree[ic[r]])
{
return r;
}
else
{
if(tree[2*k+1] >= sum)
return ask(l,(l+r)/2,2*k+1,sum);
else
{
return ask((l+r)/2+1,r,2*k+2,sum - tree[2*k+1]);
}
}
}
void update(int k)
{
tree[k] = 0;
while(k > 0)
{
k = k-1;
k/=2;
tree[k] = tree[2*k+1] + tree[2*k+2];
}
}

spoj-ORDERS - Ordering the Soldiers的更多相关文章

  1. SPOJ 227 Ordering the Soldiers

    As you are probably well aware, in Byteland it is always the military officer's main worry to order ...

  2. SPOJ 227 Ordering the Soldiers 线段树 / 树状数组

    题意:设原数组为a[i],pos[i]代表第 i 个位置之前有多少个数比a[i]大,求原数组a[i]. 这个题意是看了别人的题解才明白,我自己没读出来…… 方法:假设我们从左往右放,因为后面的数还有可 ...

  3. 树状数组求第K小值 (spoj227 Ordering the Soldiers &amp;&amp; hdu2852 KiKi&#39;s K-Number)

    题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...

  4. Ordering the Soldiers 题解

    CodeChef:ORDERS 简化题意: \(n\) 个人排队,给定每个人需要向左移动几个,求最终排列. 即还原逆序对. 错误想法 既然知道每个人向左移动 \(a_i\) 个,那就相当于让他的排名 ...

  5. poj 1731 Orders

    http://poj.org/problem?id=1731 Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9 ...

  6. [技术翻译]Guava官方文档Ordering

    关于排序 Guava的链式比较器 例子 assertTrue(byLengthOrdering.reverse().isOrdered(list)); 梗概 Ordering是Guava的链式比较器类 ...

  7. Chapter 3 -- Ordering

    Guava's fluent comparator class, Ordering, explained. explained Updated Jun 27, 2013 by cpov...@goog ...

  8. CHAPTER 19 Ordering the World 第19章 分类世界

    CHAPTER 19 Ordering the World 第19章 分类世界 Our planet is home to a bewildering variety of plants and an ...

  9. Orders

    The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the k ...

随机推荐

  1. No.1 R语言在生物信息中的应用——序列读取及格式化输出

    目的:读入序列文件(fasta格式),返回一个数据框,内容包括--存储ID.注释行(anno).长度(len).序列内容(content) 一.问题思考: 1. 如何识别注释行和序列内容行 2. 如何 ...

  2. 一款真正可以拿的出手的本土嵌入式RTOS-SylixOS

    由 winniewei 提交于 周四, 12/20/2018 作者:张国斌 在参加工信部人才交流中心和南京浦口区开发区管委会联合举办的第三届集成电路产业紧缺人才创新发展高级研修班暨产业促进交流会期间, ...

  3. Volatile的3大特性

    Volatile volatile是Java虚拟机提供的轻量级的同步机制 3大特性 1.保证可见性 当多个线程同时访问同一个变量时,一个线程修改了这个变量的值,其他线程能够立即看得到修改的值 案例代码 ...

  4. 学习java的第六天

    一.今日收获 1.开始了学习手册第二章的学习 2.了解了java里的常量与变量以及数据类型,与c语言的内容类似 二.今日难题 1.都是基础知识,没有什么难题 三.明日目标 1.继续学习java学习手册 ...

  5. Virtual Destructor

    Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...

  6. 增大Oracle Virtualbox的磁盘空间

    https://blog.csdn.net/hiyachen/article/details/102131823 背景 在virtualbox中装好Linux以及Application之后,发现硬盘空 ...

  7. Dockers启动Kafka

    首先安装 Confluent Platform Quick Start for Confluent Platform (Local install) Use this quick start to g ...

  8. SQL 父子表,显示表中每条记录所在层级

    1.sqlserer 中有一张父子关系表,表结构如下: CREATE TABLE [dbo].[testparent]( [ID] [int] IDENTITY(1,1) NOT NULL, [nam ...

  9. java多线程5:线程间的通信

    在多线程系统中,彼此之间的通信协作非常重要,下面来聊聊线程间通信的几种方式. wait/notify 想像一个场景,A.B两个线程操作一个共享List对象,A对List进行add操作,B线程等待Lis ...

  10. Mysql-5.6 二进制多实例部署

    目录 一.简介 二.环境声明 三.程序部署 一.简介 MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,通过不同的socket监听不同的服务 ...