Minimum K count

Source: Internet
Author: User
Description:

Enter n Integers to find the minimum K number. For example, if you enter the 8 numbers, the minimum four digits are, and ,.

Input:

Each test case contains two rows:

The first row contains two integers, N, and K (1 <= n, k <= 200000), indicating the length of the array.

The second row contains N integers, indicating the N number. The value range of the number in the array is [000].

Output:

For each test case, the minimum K number is output and printed in ascending order.

Sample input:
8 44 5 1 6 2 7 3 8
Sample output:
1 2 3 4
Thought 1: Use quick sorting (or other sorting) to sort the N numbers, and retrieve the first K (O (nlogn )). Idea 2: We can use the partition operation of the Quick row to solve the problem. If we adjust the K number based on the array, so that all the numbers smaller than the K number appear on its left, A number larger than this number appears on the right. In this way, K numbers on the left of the array are required. (O (n ))
Solution 2 code:
/* Minimum K count by rowandjj2014/8/9 */# include <stdio. h> # include <stdlib. h> int partition (INT arr [], int low, int high) {int val = arr [low]; while (low 
  
[Suitable for processing massive data] Train of Thought 3:Yes Create a data container with the size of K to store the minimum K numbers.Next, we will read a number from the input n integers each time. If the number in the container is less than K, we will directly put the number in the container, if the container already contains k numbers, we can only Replace the existing number instead of the new one. Find the maximum value of the existing K number, and compare the integer to be inserted with the maximum value. If the value to be inserted is smaller than the current maximum value, use this number to replace the current maximum value. If the value to be inserted is larger than the current maximum value, discard this number. Containers can use Dading heap. The root of the Big Top heap is always the largest. We only need to compare the integer to be inserted with the heap top. If it is replaced, you only need to adjust the heapadjust function (which can be used in heap sorting ).
Code:
/* Minimum K count by rowandjj2014/8/10 */# include <stdio. h> # include <stdlib. h> void heapadjust (INT arr [], int start, int end) {If (ARR = NULL | start <0 | End <= 0 | Start> = end) {return;} int temp = arr [start]; int I = start * 2 + 1; while (I <= END) {if (I + 1 <= end & arr [I + 1]> arr [I]) {I ++;} If (temp> arr [I]) {break;} arr [start] = arr [I]; Start = I; I = I * 2 + 1;} arr [start] = temp;} bool getleastnumbers (INT input [], int N, int outpu T [], int K) {If (input = NULL | output = NULL | n <= 0 | K <= 0 | K> N) {return false;} int COUNT = 0; bool needbuildheap = true; For (INT I = 0; I <n; I ++) {If (count <K) {output [count ++] = input [I];} else {// The first time you need to build the entire heap if (needbuildheap) {for (Int J = K/2-1; j> = 0; j --) // heap, starting from the first non-leaf node {heapadjust (output, J, k-1);} needbuildheap = false ;} // after the large top heap is created, compare the integer of the current traversal with the size of the heap top element if (input [I]> = output [0]) // greater than or equal to the heap top element {continue; // Discard} // if it is smaller than the heap top element, you need to switch output [0] = input [I]; heapadjust (output, 0, k-1 ); // reset to the Big Top heap} return true;} // -------------------------------------------- // because of the requirements for sorting from small to large, void heapsort (INT arr [], int Len) {If (ARR = NULL | Len <= 1) {return;} int I; for (I = Len/2-1; I> = 0; I --) {heapadjust (ARR, I, len-1) ;}for (I = len-1; I> 0; I --) {int temp = arr [0]; arr [0] = arr [I]; arr [I] = temp; heapadjust (ARR, 0, I-1) ;}} int main (){ Int N, K; while (scanf ("% d", & N, & K )! = EOF) {If (n <= 0 | K <= 0) {continue;} int output [200000]; int * arr = (int *) malloc (sizeof (INT) * n); If (! ARR) {exit (-1) ;}int I; for (I = 0; I <n; I ++) {scanf ("% d ", arr + I);} getleastnumbers (ARR, N, output, k); heapsort (output, k); for (I = 0; I <K; I ++) {if (I = k-1) {printf ("% d \ n", output [I]);} else {printf ("% d ", output [I]) ;}} free (ARR) ;}return 0 ;}


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.