For reprinting, please indicate the source: http://blog.csdn.net/ns_code/article/details/26966159
Title Description:Enter n integers and find out the smallest number of K. For example, if you input 8 digits: 4, 5, 1, 6, 2, 7, 3, 8, the smallest 4 digits are 1, 2, 3, 4.
Input:each test The case consists of two lines:
The first action is two integers n, K (1 <= n, K <= 200000), representing the length of the array.
The second line contains n integers, representing the n numbers. The range of the numbers in the array is [0,1000,000,000].
Output:For each test case, output the smallest number of k, and print in order from small to large.
Sample input:8 4
4 5 1 6 2 7 3 8Sample output:
1 2 3 4Thoughts:
1. The most intuitive way of thinking is still to sort arrays quickly and then take out the first k elements. Such time complexity is O(nlogn)
2. Similar can be used here Topic above Partition-based method, only this time the demarcation point is not the median, but the smallest k, that is, the elements that should be located in the k-1 position of the array after sorting, so that the k elements in front of the demarcation point (including the demarcation point) is the smallest K number (this K number is not necessarily sorted). The average time complexity of this method is O(n), and in the worst case it is O(n*n), just as in the analysis of the above topic, it can also be used. algorithm The method of dividing the array proposed in the introduction controls the time complexity in the worst case to O(n).
The code is as follows:
3. Consider using small-top heap to build a small-top heap of n elements of an array, so that the smallest element is located on the top of the heap and exchanged with the last element of the array, so that the smallest element is saved in the last position of the array, and then adjust the n-1 in front by using the idea of heap sorting as well. Elements, so that they constitute a small top heap again, so that after K adjustments, the smallest k elements are stored in the last K positions of the array, and increase in turn from right to left.
In this method, it takes O(n) time to build a small top heap, and then filters out the minimum number of K times to adjust the heap. The time needed for each adjustment is O(logn), O (log (n-1), O (log (n-2)... O (log (n-k). It can be approximated that the time needed for each adjustment is O(logn). In this way, the time complexity of the method is O(n+klogn). As for the space complexity, if we can change the input array, we can directly build and adjust the heap on the array. This is the space complexity of O(1). If we can not change the input array, we need to build a small top heap, so the space complexity is O(n).
This method I used on 9OJ run s, resulting in AC, coded as follows:
4. Consider using large-top heap, but not using n elements of array to build heap, but using the first k numbers to build large-top heap, and then comparing the following n-k elements with the maximum (i.e. the top) elements in large-top heap in turn. If less than the maximum element, replace the top elements with this element. And adjust the heap to maintain the structure of the large-top heap. If it is larger than the maximum element, skip directly and continue to compare the next number with the top element. When all the elements are compared and operated, then the latter elements in the array are larger than the number in the large-top heap, then the K numbers in the large-top heap become the number. The smallest K number in the array, and the top element of the heap is the largest in the smallest K array, so it is the smallest K number in the array.
The time required to build a large top heap is O(k), and the time required to adjust the heap is O(logk) each time, and the total number of adjustments is n-k, so the time complexity is as follows:
O(k+(n-k)logk), when k is much less than n, the time complexity can be approximated to O(nlogk). In addition, this algorithm is very suitable for massive data processing, especially when the memory is limited and all data can not be read in one time, when n is large and K is small, k data can be read into memory at one time, and then one comparison can be read in each time, which can meet the requirements when the memory can hold up to K data.
5. The number of K can also be saved with an array (in fact, it can be abstracted as a container. Different container choices will have different effects on the required time). The maximum value can be obtained by comparing with the following elements. The strategy similar to the fourth method is used. Finally, the smallest number of K can be saved in the array. The time complexity of this method is O(n*k).
The above two ideas and codes are no longer given.