浙江大学2022-23春夏《高级数据结构与算法分析》期末模拟练习
开始时间06/23/2024 9:07:05 AM
结束时间06/23/2024 11:07:05 AM
答题时长120分钟
考生jrguo
试卷总分100
考生得分87
判断题得分 / 总分:22 / 26
R1-1

The solution to the recurrence T(n)=T(n/6)+T(7n/9)+O(n)T(n)=T(n/6)+T(7n/9)+O(n) is O(n)O(n). (Assume T(n)=1T(n)=1 for nn smaller than some constant cc )

| 考生作答
答案
T
答案正确
2 / 2
R1-2

Suppose that a randomized algorithm A has expected running time Θ(n2)\Theta (n^2 ) on any input of size nn. Then it is possible for some execution of algorithm A to take Ω(3n)\Omega (3^n ) time.

| 考生作答
答案
T
答案正确
2 / 2
R1-3

In external sorting, the sorting is done by reading all the data into memory, sorting it there, and then writing it back out to disk.

| 考生作答
答案
F
答案正确
2 / 2
R1-4

Let nn denote the alphabet size. Huffman’s greedy algorithm requires at most log2n\log_2 n bits to encode a single symbol.

| 考生作答
答案
F
答案正确
2 / 2
R1-5

Consider the following variant of the knapsack problem. You are given kk identical knapsacks and nn items of different sizes. The profit of each item is equal to its size. Your goal is to select items and fill the knapsacks as much as possible subject to the capacity constraints. A greedy approach always packs the largest remaining item as long as there is enough room. As bin packing, if more than one knapsacks have room for the current item, one may use any packing rule (Next Fit, First Fit, and Best Fit). No matter what rule you apply, for any k1k\geq 1, the approximation ratio of the greedy algorithm is always 2.

| 考生作答
答案
T
答案正确
2 / 2
R1-6

When proving the amortized bound of a Merge operation in skew heaps, the potential of a skew heap is defined to be the total number of right heavy nodes. Then we can prove that, in an NN-node skew heap, the amortized cost for a Merge operation is exactly ​​2log2N+12\lfloor\log_2N\rfloor+1.

| 考生作答
答案
T
答案错误
0 / 2
R1-7

There are many solutions to the 5-Queens Problem, and there are 88 solutions that at least one queen is placed in a corner of the chessboard.

| 考生作答
答案
T
答案正确
2 / 2
R1-8

There exists an AVL tree of depth 6 (the depth of the root is 0) containing 31 nodes.

| 考生作答
答案
T
答案错误
0 / 2
R1-9

The following problem is in co-NP.

Given a positive integer kk, is kk a prime number?

| 考生作答
答案
T
答案正确
2 / 2
R1-10

The time complexity of merging two leftist heap can be as bad as Ω(N)\Omega(N), where NN is the number of elements in the heap.

| 考生作答
答案
F
答案正确
2 / 2
R1-11

The Random Sampling algorithm can find the maximum among nn elements in O(1)O(1) time and O(n)O(n) work. The probability of not finishing within this time and work complexity is O(1nc)O(\frac{1}{n^c}) for some positive constant cc

| 考生作答
答案
T
答案正确
2 / 2
R1-12

For any undirected graph GG, the weight of its maximum cut is at least half of the total edge weight.

| 考生作答
答案
T
答案正确
2 / 2
R1-13

Consider the two ways to implement distributed indexing. File-partitioned index is always better than the term-partitioned index.

| 考生作答
答案
F
答案正确
2 / 2
单选题得分 / 总分:51 / 60
R2-1

Given a set SS of nn numbers and a number kk between 11 and nn, consider the function Select(S,k)Select(S, k) that returns the kk-th largest element in S. We uniformly at random choose an element aia_i in S, the "splitter", and form the sets SS^{-} with items smaller than aia_i and the set S+S^{+} with items larger than aia_i. We can then determine which of SS^{-} or S+S^{+} contains the kk-th largest element, and iterate only on this one.  We say that the algorithm is in phase jj when the size of the set under consideration is at most n(3/4)jn(3/4)^j but greater than n(3/4)j+1n(3/4)^{j+1}.

Select the statement that is not correct.

| 考生作答
答案
B
答案正确
3 / 3
R2-2

Consider the maximum cut problem. Let VV be the set of vertices, let n=Vn = |V|, and let WW be the total edge weight. We have already learned a (2+ϵ)(2+\epsilon)-approximation local search algorithm that needs O(nϵlogW)O(\frac{n}{\epsilon} \log W) flips. In the following, we are trying to further reduce the number of flips by starting the algorithm with a good initial solution. Note that each vertex vv naturally forms a cut ({v},V{v})(\{v\}, V - \{v\}). Let ({v},V{v})(\{v^*\}, V - \{v^*\}) be the one with the largest weight among all such cuts. If we start the (2+ϵ)(2+\epsilon)-approximation local search algorithm with ({v},V{v})(\{v^*\}, V - \{v^*\}), how many flips do we need in the worst case?

| 考生作答
答案
C
答案错误
0 / 3
R2-3

Consider a bipartite graph G(A,B,E)G(A,B,E). (Recall that a graph GG is bipartite if the vertex set of GG can be partitioned into AA and BB such that all the edges of GG has one endpoint in AA and the other in BB.) A matching on GG is a subset MM of EE, such that no two edges in MM have a common vertex. A vertex cover is a subset CC of VV, where every edge in EE must have at least one endpoint in CC. Denote by S|S| the cardinality of a set SS. Which of the following statements is false?

| 考生作答
答案
B
答案错误
0 / 3
R2-4

Insert {7, 15, 9, 11, 5, 8, 13, 18} into an initially empty 2-3 tree (with splitting), and then delete 7. Which one of the following statements is FALSE about the resulting tree?

| 考生作答
答案
C
答案正确
3 / 3
R2-5

We are given nn jobs J1,J2,...,JnJ_1, J_2, . . . , J_n (where job ii is associated with deadlines did_i and processing times tit_i) to be scheduled on a single machine. The lateness of job ii is defined as Li=max(0,fidi)L_i = \max(0, f_i - d_i), where fif_i is the completion time of job ii. Our goal is to schedule all jobs such that L=maxiLiL = \max_i L_i is minimized.
The following greedy algorithms first sort jobs in some orders, then process the jobs in that order. Which of the following greedy algorithms always return an optimal schedule?

| 考生作答
答案
D
答案正确
3 / 3
R2-6

For the result of accessing the keys 4 and 7 in order in the splay tree given in the figure, which one of the following statements is FALSE?


cd50ffa4-e601-4ff6-b4df-e4095d114a1c.png

| 考生作答
答案
A
答案正确
3 / 3
R2-7

We have a binary counter of kk bits. Each time we conduct an increment on the counter: xx+1(mod 2k)x\equiv x+1(\text{mod} \ 2^k) and the cost of the increment is the number of bits we need to flip. For example, when k=3k=3, currently we have x=010x=010, after increment we have x=011x=011. Then this increment costs 1 because only 1 bit flips after increment. If we conduct the increment again, x=100x=100. Then this increment costs 3 because we flip 3 bits. Now we conduct nn consecutive increments and estimate the total cost. Which of the following statements are TRUE?

  1. If the inital value of the counter is 0, the total cost is O(n)O(n)

  2. If the inital value of the counter is 0, the total cost is O(nlogk)O(n\log k)

  3. If n=Ω(k)n=\Omega(k), the total cost is O(n)O(n)

  4. If n=Ω(k)n=\Omega(k), the total cost is O(nlogk)O(n\log k)

| 考生作答
答案
C
答案正确
3 / 3
R2-8

Which of the following binomial trees can represent a binomial queue of size 42?

| 考生作答
答案
A
答案正确
3 / 3
R2-9

The recurrence is T(n)=aT(n/b)+O(nc)T(n)=aT(n/b)+O(n^c) . If we considering the Binary Search as divide and conquer, what are the values of aa, bb and cc

| 考生作答
答案
B
答案正确
3 / 3
R2-10

Which of the following is a disadvantage of external sorting.

| 考生作答
答案
C
答案正确
3 / 3
R2-11

Consider three languages XX, YY and ZZ, all in NP. Suppose that XpYX\leq_{p} Y, YpZY\leq_{p} Z. Which one in the following is correct?

| 考生作答
答案
B
答案正确
3 / 3
R2-12

Delete the minimum number from the given binomial queues in the following figure. Which one of the following statements must be FALSE?


image.png

| 考生作答
答案
A
答案正确
3 / 3
R2-13

There are NN different numbers. What's the minimum time complexity to find the KK-th largest number with parallel algorithms? (No need to worry about access conflicts)

| 考生作答
答案
B
答案错误
0 / 3
R2-14

We are given nn items that are arranged in a row. From left to right, the size of items are s1,s2,,sns_1, s_2, \ldots, s_n, where 0<si10< s_i \leq 1. In addition, we require that each bin can only contain contiguous block of items with total size at most 1. That is, if items ii and jj are in the same bin, then all items between ii and jj should also be in this bin. Our goal is to pack all these items in the fewest number of bins. We adopt the classical bin packing algorithms to solve this problem, which of the following statement is true?

| 考生作答
答案
B
答案正确
3 / 3
R2-15

Merge the two skew heaps in the following figure. How many of the following statements is/are FALSE?

  • the null path length of 8 is the same as that of 12
  • 18 is the left child of 12
  • 21 is the right child of 10

7f8fa50b-d861-4bb5-96a9-5fe4bcf102ee.png

| 考生作答
答案
D
答案正确
3 / 3
R2-16

Delete the minimum number from the given skew heap. Which one of the following statements is FALSE?


image.png

| 考生作答
答案
A
答案正确
3 / 3
R2-17

When solving a problem with input size NN by divide and conquer, if at each stage the problem is divided into 77 sub-problems of equal size N/3N/3, and the conquer step takes O(logN)O(logN) to form the solution from the sub-solutions, then the overall time complexity is __.

| 考生作答
答案
C
答案正确
3 / 3
R2-18

There is an array AA, A[i]=i(1i1024)A[i]=i (1\leq i\leq 1024). If we use Balanced Binary Trees Parallel Algorithm to calculate the Prefix-Sum of AA. Define C(h,i)=k=1αA(k)C(h,i)=\sum_{k=1}^\alpha A(k), where (0,α)(0,\alpha) is the rightmost descendant leaf of node (h,i)(h,i). Node (h,i)(h,i) indicates the ii-th node in height hh. For example, the root is node (10,1)(10,1) and leaves are node (0,i) 1i1024(0,i)\ 1\leq i\leq 1024. The value of C(2,233)C(2,233) is

| 考生作答
答案
B
答案正确
3 / 3
R2-19

Below are the statistic data for one query. What are the precision and the recall for this query?


Relevant Irrelevant
Retrieved 100 200
Not Retrieved 300 400
| 考生作答
答案
D
答案正确
3 / 3
R2-20

Which of the following statements about the turnpike reconstructionis problem is true:

| 考生作答
答案
B
答案正确
3 / 3
程序填空题得分 / 总分:6 / 6
R5-1

The function ColorRBTree is to check if it is possible to color the nodes of a given binary search tree BST and turn it into a legal red-black tree. Return true if it is indeed possible, or false otherwise.
The tree structure is defined as the following:

typedef struct Node *PtrToNode;
struct Node{
    int data;
    int color; /* 1 for black and 0 for red */
    PtrToNode left, right, parent;
    int bh; /* black height */
    int npl; /* null path length */
};
typedef PtrToNode RBTree;

Please fill in the blanks.

bool ColorRBTree( RBTree BST )
{
    int H, ret = true;
    
    if (!BST->parent) {
        H = Get_Height(BST); /* get the height of BST */
        BST->color = 1;
        if (H%2)  H++; /* make tree height H an even number */
        BST->bh = H/2-1; /* the black height of the tree is at least H/2 */
        /* besides the root itself, there are at least BST->bh black nodes to be colored */
        if (BST->bh >= BST->npl)  ret = false; 
    }
    else {
         if (BST->parent->color == 0) {
             BST->color = 1;
             BST->bh = (2分);
             if (BST->bh < 0)  ret = false;
         }
         else {
             if (BST->npl > BST->parent->bh) {
                 (2分);
                 BST->bh = (2分);
             }
             else if (BST->npl == BST->parent->bh) {
                 BST->color = 1;
                 BST->bh = BST->parent->bh - 1;
             }
             else ret = false;
         }
    }
    if (ret && BST->left)
        ret = ColorRBTree(BST->left);
    if (ret && BST->right)
        ret = ColorRBTree(BST->right);
    return ret;
}
| 考生作答
填空#1
BST->parent->bh - 1
填空#2
BST->color = 0
填空#3
BST->parent->bh
| 评测详情
填空详情
序号结果得分
1答案正确2
2答案正确2
3答案正确2
答案正确
6 / 6
函数题得分 / 总分:8 / 8
R6-1

Given a sequence of K values {a1​​, a2,, aK​​}\{a​_1​​, a_2, \dots, a​_K​​\}(not necessarily integers). A continuous subsequence is defined to be{ai, ai+1​​,, aj​​}\{a_i, a_{​i+1}​​, \dots, a_j​​\} where 1ijK1≤i≤j≤K. The maximum subsequence is the subsequence that has the largest product of its elements. For example, given sequence {2,3,0.5,2,4}\{ 2, 3, 0.5, -2, 4 \}, its maximum subsequence is {2,3}\{ 2, 3 \}. The largest product is therefore 66.


Now you are supposed to find the largest product of the continuous subsequence. Hint: consider the optimal substructure of both the maximum and minimum product using your knowledge of dynamic programming.

Function Interface:

double MaxSequence(double nums[], int n);

where nums is an array of integers with n elements.

Judge Program:

#include <stdio.h>
#include <math.h>
#define MAXN 60000
#define max(x,y) ((x) > (y) ? (x) : (y))
#define min(x,y) ((x) < (y) ? (x) : (y))

double MaxSequence(double nums[], int n);

int main()
{
    double nums[MAXN];
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) scanf("%lf", &nums[i]);
    printf("%.2f", MaxSequence(nums, n));
    return 0;
}
/* Your function will be put here */

Sample Input:

5
2 3 0.5 -2 4

Sample Output:

6.00
| 考生作答
代码
编译器: CLANG
double MaxSequence(double nums[], int n){
    double prod[n];
    prod[0] = nums[0];
    for(int i=1; i<n; i++) prod[i] = prod[i-1] * nums[i];
    double f[n+5][2];  // 0 min, 1 max
    for(int i=0; i<n; i++){
        f[i][0] = 1e9;
        f[i][1] = -1e9;
    }
    f[0][0] = f[0][1] = nums[0];
    for(int i=1; i<n; i++){
        f[i][0] = f[i][1] = nums[i];
        f[i][0] = min(f[i][0], f[i-1][0] * nums[i]);
        f[i][0] = min(f[i][0], f[i-1][1] * nums[i]);
        f[i][1] = max(f[i][1], f[i-1][0] * nums[i]);
        f[i][1] = max(f[i][1], f[i-1][1] * nums[i]);
    }
    double ans = -1e9;
    for(int i=0; i<n; i++) ans = max(ans, f[i][1]);
    return ans;
}
| 评测详情
编译器输出
测试点详情
测试点结果测试点得分耗时内存
0答案正确21.00 ms184 KB
1答案正确22.00 ms360 KB
2答案正确48.00 ms1532 KB
答案正确
8 / 8