Array(1,7,2,9).sorted // 等于 Array(9,7,2,1)
时间: 2024-04-28 10:26:05 浏览: 25
Array(1,7,2,9).sorted不等于Array(9,7,2,1),Array(1,7,2,9).sorted应该返回Array(1,2,7,9)。这是一个升序排列的例子。如果要进行降序排列,可以使用sortedDescending()方法。例如,Array(1,7,2,9).sortedDescending()将返回Array(9,7,2,1)。
相关问题
https://codeforces.com/contest/1810/problem/Bhttps://codeforces.com/contest/1810/problem/B
Problem:
You are given an array a of n integers. You can perform the following operation any number of times: choose two distinct indices i and j (1≤i,j≤n) such that ai≠aj and replace ai with aj and aj with ai.
An array is called beautiful if it is sorted in non-decreasing order (a1≤a2≤…≤an).
Find the minimum number of operations needed to make the given array beautiful.
Solution:
Observation: If we can't make the array beautiful, output -1.
Let's assume we have a beautiful array as b.
If we swap two distinct indices i and j in array a, then b will also have swapped elements b[i] and b[j]. So, we need to find the minimum number of swaps required to convert array a to array b.
We can do this by counting the number of inversions in array a. An inversion is a pair of indices (i,j) such that i<j and ai>aj. The number of inversions in array a is equal to the number of swaps required to sort the array in non-decreasing order.
If the number of inversions is odd, we can't make the array beautiful.
If the number of inversions is even, then we can make the array beautiful. The minimum number of swaps required is equal to the number of inversions.
Time Complexity: O(n log n)
Space Complexity: O(n)
Implementation:
- First, we count the number of inversions in array a using merge sort.
- If the number of inversions is odd, output -1.
- Otherwise, output the number of inversions.
帮我输出A,B,C的值Input: An array A[1, ..., n], n >= 1, and k >= max(A), A= {2, 4, 0, 3, 4, 2} Output: An array B[1, ..., n], where the alements of A are sorted into ascending order. COUNTING-SORTING (A, k) 1. reserve memory for the array B[1, ..., n] 2. reserve memory for the array C[0, .., k] // Temporary auxiliary array 3. for i=0 to k 4. C[i] = 0 5. for j=1 to length(A) 6. C[A[j]] = C[A[j]]+1 7. for i=1 to k 8. C[i] = C[i]+C[i-1] 9. for j = length(A) downto 1 10. B[C[A[j]]] = A[j] 11. C[A[j]] = C[A[j]]-1 12. return B
Output: 数组B[1, ..., n],其中A的元素按升序排序。
COUNTING-SORTING (A, k)
1. 为数组B[1, ..., n]分配内存
2. 为数组C[0, .., k]分配内存 // 临时辅助数组
3. 对于i从0到k
4. C[i] = 0
5. 对于j从1到A的长度
6. C[A[j]] = C[A[j]] + 1
7. 对于i从1到k
8. C[i] = C[i] + C[i-1]
9. 对于j从A的长度到1
10. B[C[A[j]]] = A[j]
11. C[A[j]] = C[A[j]] - 1
12. 返回B
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)