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. Excel-满足指定条件并且包含数字的单元格数目,DCOUNT()

    DCOUNT函数 函数名称:DCOUNT 主要功能:返回数据库或列表的列中满足指定条件并且包含数字的单元格数目. 使用格式:DCOUNT(database,field,criteria) 参数说明:D ...

  2. “equals”有值 与 “==”存在 “equals”只是比较值是否相同,值传递,==地址传递,null==a,避免引发空指针异常,STRING是一个对象==null,对象不存在,str.equals("")对象存在但是包含字符‘''

    原文链接:http://www.cnblogs.com/lezhou2014/p/3955536.html "equals" 与 "==" "equa ...

  3. ui自动化测试,页面方法的使用

    悬浮下拉框 的设置选择 下拉框的选择 显性等待 双击, ActionChains类的方法行动链 提示框 双击,右击 双击用到行动连,提示框用到Alert的类 右击用到的也是行动连 UI自动化测试 #h ...

  4. ALitum技巧

    创建异型焊盘的方法 SCH与PCB同步修改后元器件乱跑的解决方法 Altium 在PCB重新编号更新到SCH原理图的方法 同步问题 其他技巧: 当前层亮色,其他层灰色切换:SHIFT+S

  5. 转 Android应用开发必备的20条技能

    https://blog.csdn.net/u012269126/article/details/52433237 有些andorid开发人员感觉很迷茫,接下来该去看系统源码还是继续做应用,但是感觉每 ...

  6. mysql读写分离(proxySQL) lamp+proxysql+nfs

    先在主从节点安装mysql [root@master-mariadb ~]# yum install mariadb-server -y [root@slave-mariadb ~]# yum ins ...

  7. 封装一个按Key排序的Map工具

    Map是集合的存放顺序是按哈希值定的,有时候不是我们需要的,当想要一个按自己规定顺序存放顺序,可以用LinkedHashMap,这里自己把LinkedHashMap封装了一次 package test ...

  8. Controller返回类的自动识别,WEB-INF,jsp位置

    Controller: @Controller@RequestMapping("/params")public class ParamsController { @RequestM ...

  9. 注册页面的servlet

    package cn.itcast.travel.web.servlet;import cn.itcast.travel.domain.ResultInfo;import cn.itcast.trav ...

  10. 5.Vue.js-模板语法

    Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. Vue.js 的核心是一个允许你采用简洁的模板语法来声明式的将数据渲染进 DOM 的系统 ...