import collections # 对数列按照出现频次排序 def frecuncySort(nums): count = collections.Counter(nums) # 以数字出现频次为key进行排序 vals = [(-count[k], k) for k in count] vals.sort() # 出现次数从多到少,输出对应数字个数 res = [] for val, k in vals: res += [k] * (-val) return res
# IO输入输出 N = int(input()) for i in range(N): l = input() nums = list(map(int, input().split())) nums = map(str, frecuncySort(nums)) out = ' '.join(nums) print(out)
def min_swaps(nums): # mp记录排序后数值应在的正确位置 mp = {} sort_nums = sorted(nums) for i in range(len(sort_nums)): mp[sort_nums[i]] = i
# 循环节个数 lops = 0 # 该位置的数是否被访问过 flags = [] for i in range(len(nums)): flags.append(False) for i in range(len(nums)): if not flags[i]: j = i while not flags[j]: flags[j] = True # j是当前值应在的正确位置 j = mp[nums[j]] lops += 1 return len(nums) - lops
case = int(input().strip()) while case: size = int(input().strip()) nums = list(map(int, input().strip().split())) print(min_swaps(nums)) case -= 1
public int minswap(List<Integer> nums){ List<Integer> snums = new ArrayList<>(); snums.addAll(nums); Collections.sort(snums);
// 位置数组 Map<Integer, Integer> inums = new HashMap<>(); for (int i = 0; i<snums.size(); i++) inums.put(snums.get(i), i);
int count = 0; for (int i = 0; i < nums.size(); i++){ // 最终位置 int target_i = inums.get(nums.get(i)); while (target_i != i){ int temp = nums.get(i); nums.set(i, nums.get(target_i)); nums.set(target_i, temp); count++; target_i = inums.get(nums.get(i)); } } return count; }
方法二:
在原数组中,每个元素添加一个出边指向它最终的位置,这样画完出边后,最少会成一个环,最多n个环。 每一个环对应的交换次数等于元素数量-1,因此不需要交换的次数等于环数。交换次数 = n - 环数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
python
def minswap(nums): # 排序 snums = sorted(nums) # 位置数组 inums = {snums[i]: i for i in range(len(snums))} count = 0 visited = [False] * len(nums) # 计算环数 for i in range(len(nums)): if visited[i]: continue j = i while not visited[j]: visited[j] = True j = inums[nums[j]] count += 1 return len(nums) - count
def mergeSort(nums): if len(nums) < 2: return 0, nums count = 0 mid = len(nums) // 2 cl, l = mergeSort(nums[:mid]) cr, r = mergeSort(nums[mid:]) for i in range(len(nums)): if not r or l and l[0] < r[0]: nums[i] = l.pop(0) else: nums[i] = r.pop(0) count += len(l) return count + cl + cr, nums
case = int(input().strip()) while case: size1, size2 = list(map(int, input().strip().split()))
num1 = list(map(int, input().strip().split())) num2 = list(map(int, input().strip().split())) result = [] for i in range(len(num2)): if num2[i] in num1: count = num1.count(num2[i]) for j in range(0, count): print(num2[i], end=" ") num1.remove(num2[i]) num1.sort() for i in range(len(num1)): if i == len(num1) -1: print(num1[i]) else: print(num1[i], end= " ") case -= 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
java
public List<Long> mysort(List<Long> A1, List<Long> A2){ Map<Long, Long> count = A1.stream().collect(Collectors.groupingBy(i -> i, Collectors.counting())); List<Long> res = A1.stream().filter(num -> !A2.contains(num)).collect(Collectors.toList()); Collections.sort(res); int j = 0; for (Long num: A2){ for (int i = 0; i<count.get(num); i++) { res.add(j, num); j += 1; } } return res; }