The procedure Merge(L1,L2:in List_type;L:out List_type) that we have in mind for sorting two lists is described as follows. Initialize pointers to the first item in each list L1,L2,and then
repeat
compare the two items pointed at;
move the smaller into L;
move the pointer which originally points at the smaller one to the next number;
until one of L1,L2 exhausts;
drain the remainder of the unexhausted list into L;
Now let us come to the situation when there are k pointers,here k≥2. Let L be a list of n elements. Divide L into k disjoint contiguous sublists L1,L2,…,Lk of nearly equal length. Some Li’s (namely,n reminder k of them,so possibly none) will have length
,let these have the low indices: L1,Ln%k Other Li’s will have length ,and high indices are assigned: Ln%k+1,Lk-1,Lk. We intend to recursively sort the Li’s and merge the k results into an answer list.We use Linear-Search-Merge here to merge k sorted lists. We find the smallest of k items (one from each of the k sorted source lists),at a cost of k-1 comparisons. Move the smallest into the answer list and advances its corresponding pointer (the next smallest element) in the source list from which it came. Again there are k items,from among which the smallest is to be selected. (When i (1 ≤ i
Given a list containing n elements,your task is to find out the maximum number of comparisons in k-way merging sort.
Input
The first line of the input contains an integer T (TOutput
Sample Input
42 2
3 2
100 7
1000 10
Sample Input
Case 1: 1Case 2: 3
Case 3: 1085
Case 4: 22005
题目大意:
对于归并排序,本来是2-路分治,现在要求k-路分治,求n个元素下k-路分治归并排序所需要的最大比较次数?
解题思路:
对于
注意:本题数据很大所以不能用数组存值,而用的 map 映射的。
代码如下:
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
static Map<BigInteger,BigInteger>dp = new HashMap<BigInteger,BigInteger>();
static BigInteger n,ans;
static int k;
static BigInteger dfs(BigInteger len,BigInteger x){
if(dp.containsKey(len)) return x.multiply(dp.get(len));
if(len.compareto(BigInteger.valueOf(k))<=0){
return x.multiply(len.subtract(BigInteger.ONE)).multiply(len).divide(BigInteger.valueOf(2));
}
BigInteger tmp = (BigInteger.valueOf(k).subtract(BigInteger.ONE)).multiply((len.subtract(BigInteger.valueOf(k))));
tmp = tmp.add(BigInteger.valueOf(k).multiply(BigInteger.valueOf(k).subtract(BigInteger.ONE)).divide(BigInteger.valueOf(2)));
BigInteger kk = len.mod(BigInteger.valueOf(k));
if(kk!=BigInteger.ZERO){
tmp=tmp.add(dfs(len.divide(BigInteger.valueOf(k)).add(BigInteger.ONE),kk));
}
tmp = tmp.add(dfs(len.divide(BigInteger.valueOf(k)),BigInteger.valueOf(k).subtract(kk)));
dp.put(len,tmp);
return tmp.multiply(x);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for(int cas=1; cas<=T; cas++){
dp.clear();
n = in.nextBigInteger();
k = in.nextInt();
ans=dfs(n,BigInteger.ONE);
System.out.println("Case "+cas+": "+ans);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。