• 黑盒子问题


    一 问题描述

    黑盒子代表一个原始数据库,存储一个整数数组和一个特殊的 i 变量。最初的时刻,黑盒子是空的,i=0,黑盒子处理一系列命令(事务)。有两种类型的事务。

    ① ADD(x),将元素 x 放入黑盒子中。

    ② GET,将 i 增加1,并给出包含在黑盒子中的所有整数中第 i 小的值。第 i 小的值是黑盒子中按非降序排序后第 i个位置的数字。

    示例如下:

    写一个有效的算法来处理给定的事务序列。ADD 和 GET事务的最大数量均为 30000,用两个整数数组来描述事务的顺序:

    ① A(1), A(2),…,A(M ),包含黑盒子中的一系列元素,A 值是绝对值不超过 2 000 000 000 的整数,M≤30000,对上面的示例,序列A=(3, 1,-4, 2,8,-1000, 2);

    ② u(1), u(2), …,u (N),表示在第 1 个、第 2 个,以此类推,直到第 N 个 GET事务时包含在黑盒子中的元素个数。对上面的示例,u=(1, 2, 6, 6)。假设自然数序列 u(1), u(2), …, u(N ) 按非降序排序,则对 u 序列的第 p 个元素执行 GET 事务,实际上是找 A(1), A(2), …, A(u(p)) 序列中第 p 小的数。

    二 输入和输出

    1 输入

    输入包含(按给定顺序)M , N , A(1), A(2), …, A(M), u (1), u (2), …, u (N )。

    2 输出

    按照给定的事务顺序输出答案序列,每行一个数字。

    三 输入和输出样例

    1 输入样例

    7 4

    3 1 -4 2 8 -1000 2

    1 2 6 6

    2 输出样例

    3

    3

    1

    2

    四 分析和设计

    可以创建平衡二叉树,查找第 k 小,采用 Treap 解决。

    本问题要控制黑盒子中的元素数量,然后查询第 k 小。u =(1, 2, 6, 6),在黑盒子中有 1 个数时查询第 1 小;在黑盒子中有 2 个数时查询第 2小;在黑盒子中有 6 个数时查询第 3 小;在黑盒子中有 6 个数时查询第 4 小。

    五 代码

    1. package com.platform.modules.alg.alglib.poj1442;
    2. import java.util.Random;
    3. public class Poj1442A {
    4. public String output = "";
    5. private int maxn = 30010;
    6. int num[] = new int[maxn];
    7. int num1[] = new int[maxn];
    8. int n, cnt, root; //结点数,结点存储下标累计,树根
    9. private node tr[] = new node[maxn];
    10. public Poj1442A() {
    11. for (int i = 0; i < tr.length; i++) {
    12. tr[i] = new node();
    13. }
    14. }
    15. public String cal(String input) {
    16. int n, a, b, m;
    17. String[] line = input.split("\n");
    18. String[] nums = line[0].split(" ");
    19. n = Integer.parseInt(nums[0]);
    20. m = Integer.parseInt(nums[1]);
    21. String[] elements = line[1].split(" ");
    22. String[] element1s = line[2].split(" ");
    23. root = 0;
    24. for (int i = 1; i <= n; i++) {
    25. num[i] = Integer.parseInt(elements[i - 1]);
    26. }
    27. for (int i = 1; i <= m; i++) {
    28. num1[i] = Integer.parseInt(element1s[i - 1]);
    29. }
    30. int t = 1, k = 1;
    31. while (t <= m) {
    32. while (k <= num1[t]) {
    33. root = Insert(root, this.num[k]);
    34. k++;
    35. }
    36. int ans = Findkth(root, t++);
    37. output += ans + "\n";
    38. }
    39. return output;
    40. }
    41. // 生成新结点
    42. int New(int val) {
    43. tr[++cnt].val = val;
    44. tr[cnt].pri = Math.abs(new Random().nextInt()) % 100;
    45. tr[cnt].num = tr[cnt].size = 1;
    46. tr[cnt].rc = tr[cnt].lc = 0;
    47. return cnt;
    48. }
    49. // 更新子树大小
    50. void Update(int p) {
    51. tr[p].size = tr[tr[p].lc].size + tr[tr[p].rc].size + tr[p].num;
    52. }
    53. // 右旋
    54. int zig(int p) {
    55. int q = tr[p].lc;
    56. tr[p].lc = tr[q].rc;
    57. tr[q].rc = p;
    58. tr[q].size = tr[p].size;
    59. Update(p);
    60. // 现在 q 为根
    61. p = q;
    62. return p;
    63. }
    64. // 左旋
    65. int zag(int p) {
    66. int q = tr[p].rc;
    67. tr[p].rc = tr[q].lc;
    68. tr[q].lc = p;
    69. tr[q].size = tr[p].size;
    70. Update(p);
    71. // 现在 q 为根
    72. p = q;
    73. return p;
    74. }
    75. // 在 p 的子树插入值 val
    76. int Insert(int p, int val) {
    77. if (p == 0) {
    78. p = New(val);
    79. return p;
    80. }
    81. tr[p].size++;
    82. if (val == tr[p].val) {
    83. tr[p].num++;
    84. return p;
    85. }
    86. if (val < tr[p].val) {
    87. tr[p].lc = Insert(tr[p].lc, val);
    88. if (tr[p].pri < tr[tr[p].lc].pri)
    89. p = zig(p);
    90. } else {
    91. tr[p].rc = Insert(tr[p].rc, val);
    92. if (tr[p].pri < tr[tr[p].rc].pri)
    93. p = zag(p);
    94. }
    95. return p;
    96. }
    97. // 求第 k 小的数
    98. int Findkth(int p, int k) {
    99. if (p == 0) return 0;
    100. int t = tr[tr[p].lc].size;
    101. if (k < t + 1) return Findkth(tr[p].lc, k);
    102. else if (k > t + tr[p].num) return Findkth(tr[p].rc, k - (t + tr[p].num));
    103. else return tr[p].val;
    104. }
    105. }
    106. class node {
    107. int lc, rc; // 左右孩子
    108. int val, pri; // 值,优先级
    109. int num, size; // 重复个数,根的子树的大小
    110. }

     六 测试

  • 相关阅读:
    设计模式之创建型模式:原型模式
    智工教育:公务员网上报名确认事项与流程的状态标识
    12108 - Extraordinarily Tired Students (UVA)
    NPDP产品经理国际认证报名有什么要求?
    HTTP Header 参数详解
    APT级全面免杀与企业纵深防御体系的红蓝对抗
    修谱是一件好事:薪火相传,让老一辈庇护和提携后辈,造福乡里宗亲
    金仓数据库KingbaseES客户端编程接口指南-ado.net(5. 连接串参数)
    (二)激光线扫描-相机标定
    AnyCast技术
  • 原文地址:https://blog.csdn.net/chengqiuming/article/details/127982130