The minimum number of K for "The sword means offer"

Source: Internet
Author: User
Tags random seed

Reprint Please specify the Source: http://blog.csdn.net/ns_code/article/details/26966159

Topic Description Narration:

Enter n integers to find the smallest number of K. For example, enter the 8 numbers of 4,5,1,6,2,7,3,8. The smallest 4 digits are 1,2,3,4.

Input:

Each test case consists of 2 lines:

The first behavior is 2 integers n,k (1<=n,k<=200000), which represents the length of the array.

The second line consists of n integers. Represents this n number, and the range of the number in the array is [0,1000 000 000].

Output:

corresponding to each test case, the output of the smallest number of K. and print from small to large order.

Example input:
8 4
4 5 1 6 2 7 3 8
Example output:
1 2 3 4
Ideas:

1, the most intuitive thinking is still an array of high-speed sequencing. Then remove the first k elements. This time complexity is O (NLOGN)

2, here can be used similar to the above question for the purpose of partition-based approach, is only the demarcation point of the request is not the median, but the number of k small, that is, the order should be located in the k-1 position of the element in the array, The K-element (including the cutoff point) in front of the cutoff point is the smallest number of K (the K-numbers are not necessarily ordered). The average time complexity of this method is O (n) and the time Complexity of O (n*n) in the worst case, as well as the method of cutting arrays proposed in the introduction of the algorithm, as in the above topic analysis. Control the worst-case time complexity to O (n).

The code is as follows:

#include <stdio.h> #include <stdlib.h> #include <time.h>void Swap (int *a,int *b) {if (*a! = *b) {*a = *a + *b ; *b = *a-*b;*a = *a-*b;}} /* Algorithm Introduction version Partition function */int Partition (int *a,int low,int high) {if (A==null | | low<0 | | high<0 | | low>=high) return -1;int small = Low-1;int J;for (j=low;j 3.It is possible to consider using a small top heap to build n elements of an array into a small top heap, so that the smallest element is at the top of the heap, exchanging it with the last element of the array. So the smallest element is saved in the last position of the array. Then the same idea of using heap sequencing. Adjust the previous n-1 elements so that they form a small top heap again, so that the smallest K-elements are saved in the last K-position of the array after the K-adjustment, and are incremented from right to left.

Such a method. It takes O (n) time to set up a small top heap, and then the minimum number of K needs to be adjusted to the heap k times. The time required for each adjustment is O (logn), O (log (n-1)), O (log (n-2)) ... O (log (n-k)). Can approximate the time required for each adjustment is O (logn). In this way, the time complexity of the method is O (N+klogn), as for the spatial complexity. Assuming the ability to change the input array, we can build and adjust the heap directly on the array. This is the space complexity of O (1). Assuming we can't change the input array, we're going to build a small top heap. This space complexity is O (n).

I used this method on the nine-degree OJ run, resulting in AC, code such as the following:

#include <stdio.h> #include <stdlib.h>/*arr[start+1...end] to meet the definition of a small top heap, add arr[start] to the small top heap Arr[start+1...end ], adjust the position of the arr[start], so that arr[start...end] also become a small top heap note: Because the array starts from 0 to calculate the sequence number, that is, two fork heap root node ordinal is 0, so the sequence number of the left and right child nodes of I are 2i+1 and 2i+2*/void Heapadjustdown (int *arr,int start,int end) {int temp = arr[start];//Saves the current node int i = 2*start+1;//the position of the left child of the node in the array ordinal while (i <=end) {//Find the smallest of the left and right children if (I+1<=end && arr[i+1]<arr[i]) i++;//assume that the definition of the heap is met, then do not adjust the position if (arr[i]>=temp) break;//the smallest child node moves up, replacing its parent node Arr[start] = Arr[i];start = I;i = 2*start+1;} Arr[start] = temp;} /* Get the minimum number of K, save the Last face in arr k position */void minheapknum (int *arr,int len,int k) {if (Arr==null | | len<1 | | k<1 | | k>len) Return;int i;//Set the number of small top heap//The first non-leaf node position sequence number is (len-1)/2for (i= (len-1)/2;i>=0;i--) Heapadjustdown (arr,i,len-1);// Make heap sort for (i=len-1;i>=len-k;i--) {//heap top element and last element swap location. So the last position is the smallest number,//each time the loop in turn, the smaller value is placed in the previous position. int temp = Arr[i];arr[i] = arr[0];arr[0] = temp;//will arr[0...i-1] again adjust to the small top heap heapadjustdown (arr,0,i-1);}} int main () {int n,k;while (scanf ("%d%d", &n,&k)! = EOF) {int *a = (int *) malloc (sizeof (int) *n), if (A = = NULL) exit (exit_failure); int i;for (i=0;i<n;i++) scanf ("% D ", a+i); Minheapknum (a,n,k); for (i=n-1;i>=n-k;i--) {//output if (i = = n-k) printf ("%d\n", A[i]) in accordance with the required format, elseprintf ("%d", A[i]);}} return 0;}
/**************************************************************&NBSP;&NBSP;&NBSP;&NBSP; problem:1371 &NBSP;&NBSP;&NBSP;&NBSP; user:mmc_maodun &NBSP;&NBSP;&NBSP;&NBSP; language:c &NBSP;&NBSP;&NBSP;&NBSP; result:accepted &NBSP;&NBSP;&NBSP;&NBSP; time:840 Ms &NBSP;&NBSP;&NBSP;&NBSP; memory:8752 KB ****************************************************************/

4, can also consider the use of large top piles. But it does not use the n elements of the array to build a heap. Instead, use the first k numbers to build a large top pile. Then take the back of the back of the n-k elements in turn and the big top heap in the maximum (that is, the heap top) element comparison. If it is less than the maximum element, the top element of the heap is replaced with that element. and adjust the heap to maintain the structure of the large top heap, assuming greater than the maximum element, then skip directly, continue to take a number compared to the top of the heap elements. When all the elements are compared and manipulated, the subsequent elements in the array are larger than the numbers in the large top heap. Then the number of K in the Big Top heap becomes the smallest k number in the array, and the top element of the heap is the largest of the K smallest array, so it is a small number of k in the array.

The algorithm establishes a large top heap with an O (k), and the time required for each adjustment of the heap is O (logk). And a total of adjustments to n-k times. So the complexity of time is

O (k + (N-K) logk), when K is far less than N, the time complexity can be approximated to O (NLOGK). In addition, the algorithm is very suitable for mass data processing, especially in memory limited. You cannot read all the data at once. When n is very large, and K is smaller, the memory is read into the K data at a time. Then each time can read into a comparison. This satisfies the requirement for memory to hold up to K data.

5, can also use the array to save the number of K (in fact, can be abstracted into a container, the different choice of container, the time required will have different effects), the maximum value. Compared with the following elements, using a strategy similar to the one in the 4th, the smallest number of K is saved in the array. The time complexity of such a method is O (n*k).

The above two ideas code is no longer given.



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

The minimum number of K for "The sword means offer"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.