spoj-ORDERS - Ordering the Soldiers
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的更多相关文章
- SPOJ 227 Ordering the Soldiers
As you are probably well aware, in Byteland it is always the military officer's main worry to order ...
- SPOJ 227 Ordering the Soldiers 线段树 / 树状数组
题意:设原数组为a[i],pos[i]代表第 i 个位置之前有多少个数比a[i]大,求原数组a[i]. 这个题意是看了别人的题解才明白,我自己没读出来…… 方法:假设我们从左往右放,因为后面的数还有可 ...
- 树状数组求第K小值 (spoj227 Ordering the Soldiers && hdu2852 KiKi's K-Number)
题目:http://www.spoj.com/problems/ORDERS/ and pid=2852">http://acm.hdu.edu.cn/showproblem.php? ...
- Ordering the Soldiers 题解
CodeChef:ORDERS 简化题意: \(n\) 个人排队,给定每个人需要向左移动几个,求最终排列. 即还原逆序对. 错误想法 既然知道每个人向左移动 \(a_i\) 个,那就相当于让他的排名 ...
- poj 1731 Orders
http://poj.org/problem?id=1731 Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9 ...
- [技术翻译]Guava官方文档Ordering
关于排序 Guava的链式比较器 例子 assertTrue(byLengthOrdering.reverse().isOrdered(list)); 梗概 Ordering是Guava的链式比较器类 ...
- Chapter 3 -- Ordering
Guava's fluent comparator class, Ordering, explained. explained Updated Jun 27, 2013 by cpov...@goog ...
- CHAPTER 19 Ordering the World 第19章 分类世界
CHAPTER 19 Ordering the World 第19章 分类世界 Our planet is home to a bewildering variety of plants and an ...
- Orders
The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the k ...
随机推荐
- 学习java 7.3
学习内容:定义类不需要加static 成员方法在多个对象时是可以共用的,而成员变量不可以共用,多个对象指向一个内存时,改变变量的值,对象所在的类中的变量都会改变 成员变量前加private,成员方法前 ...
- tomcat拦截特殊字符报400,如 "|" "{" "}" ","等符号的解决方案
最近在做一个项目,需要对外暴露两个接口接收别人给的参数,但是有一个问题就是对方的项目是一个老项目,在传参数的时候是将多个字符放在一个参数里面用"|"进行分割,然而他们传参数的时候又 ...
- day07 Linux配置修改
day07 Linux配置修改 昨日回顾 1.系统目录 /etc :系统配置目录 /bin-> /usr/bin :保存常用命令的目录 /root :超级管理员目录 /home :普通管理员目录 ...
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- 如何让Linux 机器CPU使用率变高
如何让Linux 机器CPU使用率变高 一.实现 1.单行命令搞定 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" ...
- 移动端(App)项目进行满屏高度的设置
做移动端App的时候 高度一般会根据页面的元素进行自动设置,不会铺满整个屏幕.通过以下代码实现满屏高度. #app{ width: 100%; height: 100%; position: abso ...
- java 整型
byte(1字节).short(2字节).int(4字节).long(16字节) java中前缀加上0b或者0B就可以写二进制数,前缀加上0就可以写八进制数,前缀加上0x或者0X就可以写十六进制数 一 ...
- 【.NET 与树莓派】控制彩色灯带(WS28XX)
彩色灯带,相信不用老周多说,大家都知道,没准你家里的灯墙里面就有.老周的茅屋是早期建造的,所以没有预留的灯槽,明灯的话是不好看的,因此老周家里没使用灯带.不过,像柜子后面,显示器后面,书桌边沿这些地方 ...
- Jenkins制品管理
目录 一.简介 二.Jenkins管理制品 三.Nexus maven上传 jenkins上传 管理Docker镜像 管理raw 四.拷贝制品 五.版本号 Version Number 一.简介 制品 ...
- react-hook简单使用
一.函数式组件创建 function HelloComponent(props, /* context */) { return <div>Hello {props.name}</d ...