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. 用jquery的prop方法操作checkbox

    prop设置checkbox选中 $('#checkbox-id').prop("checked",true) 判断checkbox是否选中,if ($('#checkbox-id ...

  2. C#序号

    OnRowCreated="gridViewCorrection_RowCreated" <asp:BoundField HeaderText="序号" ...

  3. Kubernetes:应用自动扩容、收缩与稳定更新

    在前面我们已经学习到了 Pod 的扩容.滚动更新等知识,我们可以手动为 Deployment 等设置 Pod 副本的数量,而这里会继续学习 关于 Pod 扩容.收缩 的规则,让 Pod 根据节点服务器 ...

  4. flink---实时项目----day03---1.练习讲解(全局参数,数据以parquet格式写入hdfs中) 2 异步查询 3 BroadcastState

    1 练习讲解(此处自己没跑通,以后debug) 题目见flink---实时项目---day02 kafka中的数据,见day02的文档 GeoUtils package cn._51doit.flin ...

  5. 02-爬取http://www.allitebooks.org/网站,获取图片url,书名,简介,作者

    import requests from lxml import etree from bs4 import BeautifulSoup import json class BookSpider(ob ...

  6. 从源码看RequestMappingHandlerMapping的注册与发现

    1.问题的产生 日常开发中,大多数的API层中@Controller注解和@RequestMapping注解都会被使用在其中,但是为什么标注了@Controller和@RequestMapping注解 ...

  7. 调试器gdb

    1.启动和退出gdb gdb调试的对象是可执行文件,而不是程序源代码.如果要使一个可执行文件可以被gdb调试,那么在使用编译器gcc编译程序时加入-g选项.-g选项告诉gcc在编译程序时加入调试信息, ...

  8. 理解各种不同含义的 new 和 delete

    new operator new操作符 operator new 操作符new placement new 定位new string *ps = new string("Memory Man ...

  9. References in C++

    When a variable is declared as reference, it becomes an alternative name for an existing variable. A ...

  10. ES6常用的数值转换方法

    <script type="text/javascript"> // Number常用方法 /* Number.isFinite() 用来检查一个数值是否为有限的(fi ...