ORDERS - Ordering the Soldiers

  1. 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.
  2. 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.
  3. 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.
  4. 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?
  5. Input
  6. 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.
  7. Output
  8. 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.
  9. Example
  10. Input:
  11. 2
  12. 3
  13. 0 1 0
  14. 5
  15. 0 1 2 0 1
  16. Output:
  17. 2 1 3
  18. 3 2 1 5 4

思路:线段树;

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

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<queue>
  5. #include<string.h>
  6. #include<map>
  7. typedef long long LL;
  8. using namespace std;
  9. int id[300000];
  10. int tree[4*200005];
  11. int ic[300000];
  12. int ac[300000];
  13. void build(int l,int r,int k);
  14. int ask(int l,int r,int k,int sum);
  15. void update(int k);
  16. int main(void)
  17. {
  18. int T;
  19. scanf("%d",&T);
  20. while(T--)
  21. {
  22. int n;
  23. scanf("%d",&n);
  24. for(int i = 0; i < n; i++)
  25. scanf("%d",&id[i]);
  26. build(1,n,0);int cn = 0;
  27. int p = n;
  28. for(int i = n-1;i >= 0;i--)
  29. {
  30. ac[cn] = ask(1,n,0,p-id[i]);
  31. p--;
  32. update(ic[ac[cn]]);
  33. cn++;
  34. }
  35. printf("%d",ac[n-1]);
  36. for(int i = n-2;i >= 0;i--)
  37. {
  38. printf(" %d",ac[i]);
  39. }
  40. printf("\n");
  41. }
  42. return 0;
  43. }
  44. void build(int l,int r,int k)
  45. {
  46. if(l == r)
  47. {
  48. tree[k] = 1;
  49. ic[l] = k;
  50. return ;
  51. }
  52. build(l,(l+r)/2,2*k+1);
  53. build((l+r)/2+1,r,2*k+2);
  54. tree[k] = tree[2*k+1] + tree[2*k+2];
  55. }
  56. int ask(int l,int r,int k,int sum)
  57. {
  58. if(tree[k] == sum&&tree[ic[r]])
  59. {
  60. return r;
  61. }
  62. else
  63. {
  64. if(tree[2*k+1] >= sum)
  65. return ask(l,(l+r)/2,2*k+1,sum);
  66. else
  67. {
  68. return ask((l+r)/2+1,r,2*k+2,sum - tree[2*k+1]);
  69. }
  70. }
  71. }
  72. void update(int k)
  73. {
  74. tree[k] = 0;
  75. while(k > 0)
  76. {
  77. k = k-1;
  78. k/=2;
  79. tree[k] = tree[2*k+1] + tree[2*k+2];
  80. }
  81. }

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. Dango之form校验组件

    目录 1.引入案例 2. form组件的功能 3. form组件的使用 3.1 自定义form校验类 3.2 校验数据 3.3 渲染页面 3.4 展示错误信息 3.5 自定义校验结果 3.6 form ...

  2. Volatile的3大特性

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

  3. linux vi和vim编辑器

    所有的Linux系统都会内建vi文本编辑器,vim具有程序编辑的能力,可以看作是vi的增强版本 三种常见模式 正常模式 以vim打开一个文档直接进入的模式,快捷键可以使用. 1.这个模式可以使用上下左 ...

  4. abandon, abbreviation

    abandon 近/反义词: continue, depart, desert (做动词时读作diˈzəːt), discard, give up, quit, surrender搭配: altoge ...

  5. Learning Spark中文版--第三章--RDD编程(1)

       本章介绍了Spark用于数据处理的核心抽象概念,具有弹性的分布式数据集(RDD).一个RDD仅仅是一个分布式的元素集合.在Spark中,所有工作都表示为创建新的RDDs.转换现有的RDD,或者调 ...

  6. Shell学习(八)——dd命令

    一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...

  7. MySQL压力测试工具

    一.MySQL自带的压力测试工具--Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新 ...

  8. Activity 详解

    1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...

  9. 【编程思想】【设计模式】【行为模式Behavioral】模板模式Template

    Python转载版 https://github.com/faif/python-patterns/blob/master/behavioral/template.py #!/usr/bin/env ...

  10. Java易错小结

    String 相关运算 String使用是注意是否初始化,未初始化的全部为null.不要轻易使用 string.isEmpty()等,首先确保string非空. 推荐使用StringUtils.isN ...