The solution to the recurrence is . (Assume for smaller than some constant )
Suppose that a randomized algorithm A has expected running time on any input of size . Then it is possible for some execution of algorithm A to take time.
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.
Let denote the alphabet size. Huffman’s greedy algorithm requires at most bits to encode a single symbol.
Consider the following variant of the knapsack problem. You are given identical knapsacks and 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 , the approximation ratio of the greedy algorithm is always 2.
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 -node skew heap, the amortized cost for a Merge operation is exactly .
There are many solutions to the 5-Queens Problem, and there are solutions that at least one queen is placed in a corner of the chessboard.
There exists an AVL tree of depth 6 (the depth of the root is 0) containing 31 nodes.
The following problem is in co-NP.
Given a positive integer , is a prime number?
The time complexity of merging two leftist heap can be as bad as , where is the number of elements in the heap.
The Random Sampling algorithm can find the maximum among elements in time and work. The probability of not finishing within this time and work complexity is for some positive constant .
For any undirected graph , the weight of its maximum cut is at least half of the total edge weight.
Consider the two ways to implement distributed indexing. File-partitioned index is always better than the term-partitioned index.
Given a set of numbers and a number between and , consider the function that returns the -th largest element in S. We uniformly at random choose an element in S, the "splitter", and form the sets with items smaller than and the set with items larger than . We can then determine which of or contains the -th largest element, and iterate only on this one. We say that the algorithm is in phase when the size of the set under consideration is at most but greater than .
Select the statement that is not correct.
Consider the maximum cut problem. Let be the set of vertices, let , and let be the total edge weight. We have already learned a -approximation local search algorithm that needs 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 naturally forms a cut . Let be the one with the largest weight among all such cuts. If we start the -approximation local search algorithm with , how many flips do we need in the worst case?
Consider a bipartite graph . (Recall that a graph is bipartite if the vertex set of can be partitioned into and such that all the edges of has one endpoint in and the other in .) A matching on is a subset of , such that no two edges in have a common vertex. A vertex cover is a subset of , where every edge in must have at least one endpoint in . Denote by the cardinality of a set . Which of the following statements is false?
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?
We are given jobs (where job is associated with deadlines and processing times ) to be scheduled on a single machine. The lateness of job is defined as , where is the completion time of job . Our goal is to schedule all jobs such that 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?
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?
We have a binary counter of bits. Each time we conduct an increment on the counter: and the cost of the increment is the number of bits we need to flip. For example, when , currently we have , after increment we have . Then this increment costs 1 because only 1 bit flips after increment. If we conduct the increment again, . Then this increment costs 3 because we flip 3 bits. Now we conduct consecutive increments and estimate the total cost. Which of the following statements are TRUE?
If the inital value of the counter is 0, the total cost is
If the inital value of the counter is 0, the total cost is
If , the total cost is
If , the total cost is
Which of the following binomial trees can represent a binomial queue of size 42?
The recurrence is . If we considering the Binary Search as divide and conquer, what are the values of , and ?
Which of the following is a disadvantage of external sorting.
Consider three languages , and , all in NP. Suppose that , . Which one in the following is correct?
Delete the minimum number from the given binomial queues in the following figure. Which one of the following statements must be FALSE?
There are different numbers. What's the minimum time complexity to find the -th largest number with parallel algorithms? (No need to worry about access conflicts)
We are given items that are arranged in a row. From left to right, the size of items are , where . In addition, we require that each bin can only contain contiguous block of items with total size at most 1. That is, if items and are in the same bin, then all items between and 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?
Merge the two skew heaps in the following figure. How many of the following statements is/are FALSE?
Delete the minimum number from the given skew heap. Which one of the following statements is FALSE?
When solving a problem with input size by divide and conquer, if at each stage the problem is divided into sub-problems of equal size , and the conquer step takes to form the solution from the sub-solutions, then the overall time complexity is __.
There is an array , . If we use Balanced Binary Trees Parallel Algorithm to calculate the Prefix-Sum of . Define , where is the rightmost descendant leaf of node . Node indicates the -th node in height . For example, the root is node and leaves are node . The value of is
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 |
Which of the following statements about the turnpike reconstructionis problem is true:
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 | 答案正确 | 2 |
2 | 答案正确 | 2 |
3 | 答案正确 | 2 |
Given a sequence of K values (not necessarily integers). A continuous subsequence is defined to be where . The maximum subsequence is the subsequence that has the largest product of its elements. For example, given sequence , its maximum subsequence is . The largest product is therefore .
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.
double MaxSequence(double nums[], int n);
where nums
is an array of integers with n
elements.
#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 */
5
2 3 0.5 -2 4
6.00
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 | 答案正确 | 2 | 1.00 ms | 184 KB |
1 | 答案正确 | 2 | 2.00 ms | 360 KB |
2 | 答案正确 | 4 | 8.00 ms | 1532 KB |