问题中描述的问题是否可以解决? [关闭]

问题描述 投票:0回答:0

Note: Ignore the obvious printing mistake

将图像转换为文本:

a) 一个人的包里有 n 个苹果 A[],其中 A[i] 是第 i 个苹果的大小。假设没有两个苹果大小相同。这个人不会告诉你任何苹果的大小。然而,这个人可以回答 IsLargeApples(i,j) 形式的查询,它根据 A[i] 大于 A[j] 还是 A[i] 小于 A[j] 返回 1, -1,分别。编写一个程序来创建一个索引数组 S[] 使得 {A[S[0]], < A[S[1]], < A[S]2]] ... < A[S[n-1]]} is the sorted sequence of the apples in the order of their sizes. Modify merge sort to solve your problem. Your modification should use the array S[] to compare sizes of the apples. Here you move/swap/copy the indices in stead of the sizes. For example, if S[i] = j, then A[S[i]] is the jth apple A, (or the size of the j-th apple). But you cannot access A[] or its size, only IsLargeApples(i,j) can access the array A[]. However, you are free to make any modification to your own array S.

b) 这个人也有 n 个盒子 B[]。给定对于每个苹果 A[i] 都有一个唯一的盒子 B[j] 使得 A[i] 紧密地适合 B[j]。但是 A[i] 也可以装进一个更大的盒子,但装不下一个更小的盒子。编写一个程序,找到苹果和盒子的精确匹配,也就是说,你需要创建一个数组 M[],使得球 A[i] 与盒子 B[M[i]] 完全匹配。在 a) 部分,您已经根据大小对苹果进行了排序。现在,如果你能对盒子进行排序,你就能找到匹配的。不幸的是,这个人不会告诉你任何关于盒子尺寸比较的信息。相反,他将回答 TightFit(i,j) 形式的查询。如果框 B[j] 与球 A[i] 完全匹配,则答案为 0。如果 A[i] 适合 B[j] 但不紧,则答案为 1。如果苹果 A[i] 比盒子 B]j] 大,则答案为 -1。使用修改后的版本创建 M 快速排序和合并排序。您可以使用也可以不使用第 1 部分中提供的数组 S。现在,使用 TightFit() 查询,但不允许使用 IsLargeApples() 查询。

请帮忙。这个问题可以解决吗?

algorithm sorting mergesort divide-and-conquer
© www.soinside.com 2019 - 2024. All rights reserved.