3 kth
3.1 Description
Given n a positive integer that does not exceed 10^9, the linear time-selection algorithm (linear Select) asks for the K-value.
3.2 Input
The first row of two integers n,k. The second row n integers, which represent the n positive integers in the title.
3.3 Output
A line that represents the answer.
3.4 Sample Input
10 3
2 4 7 3 5 6 9 6 1 8
3.5 Sample Output
7
3.6 Constraints
A total of 10 test points, each test point 10 points, only if your answer is exactly the same as the standard answers to get 10 points, otherwise 0 points. For 30% of data, n≤1000. For 50% of data, n≤100000. For 100% of data, 1≤n≤106,1≤k≤n. Time limit: 1s/256m
By the Great God's linear select God, and today he wrote a quick select together with%%% Captain%%%w teacher
The idea of linear Select is to divide a sequence into 5 groups of intervals, take the median of each interval, and then take the median of these median, there is proof that it is not less than interval 1/4, not greater than the interval 3/4, so that we reduce the problem size, at the same time, the [median of the median number] We can still use an algorithm that asks for the interval K-Large (small) number so that we can use the recursive structure.
1 //Linear Select O (100n)2#include <stdio.h>3#include <stdlib.h>4 5 Const intMAXN =1000005;6 7 intA[MAXN];8 9 int__cnt;Ten OneInlinevoidSortint*a,intN) {//to prove that this algorithm is very efficient, the teacher used the slowest choice to sort%%% A for(inti =1; I < n; i++) { - for(intj = i +1; J <= N; J + +) { -++__cnt; the if(A[i] >A[j]) { - intTMP =A[i]; -A[i] =A[j]; -A[J] =tmp; + } - } + } A } at - intTrivial_select (int*a,intNintK) {//to sort an interval, take the median - sort (A, n); - returnA[k]; - } - in //a[]: 1-based - int Select(int*a,intNintk) { to Static Const intQ =5;//dividing the interval to take odd numbers + if(N <=Q) { - returnTrivial_select (A, n, k); the } * int*b =New int[(N-1)/Q +1]; $ intC[q];Panax Notoginseng intCNT = (N-1)/Q +1; - for(inti =0; I < CNT; i++) { the for(intj =0; J < Q; J + +) { +C[J] = i * Q + j < n? A[i * Q + j +1] :2147483647; A } theB[i] = Trivial_select (C-1, q, (q +1) /2); + } - intx =Select(B-1, CNT, (CNT +1) /2);//The median number of the median in the interval $ Delete[]b; $ - Static intZ[MAXN]; - intL =0, R = n +1; the for(inti =1; I <= N; i++) { - if(A[i] <x) {WuyiZ[++L] =A[i]; the}Else { -Z[--R] =A[i]; Wu } - } About for(inti =1; I <= N; i++) { $A[i] =Z[i]; - } - if(k <=l) { - return Select(A, l, K); A}Else { + return Select(A + L, n-l, K-l); the } - } $ the intMain () { theFreopen ("kth.in","R", stdin); theFreopen ("Kth.out","W", stdout); the intN, K; -scanf"%d%d", &n, &k); in for(inti =1; I <= N; i++) { thescanf"%d", A +i); the } Aboutprintf"%d\n",Select(A, n, N-k +1)); the}
Quick Select is similar to the idea of a quick row, a random selection of the number after the fast row to determine the position, the number is smaller than it on its left, larger than its number is on its right, so that the problem can be reduced to the scale, recursive solution, the data can be randomly reached excepted O (NLOGN) (constant unknown)
#include <cstdio>#include<iostream>#include<algorithm>#include<cstdlib>#include<ctime>#defineSiji (i,x,y) for (int i= (x); i<= (y); i++)#defineGongzi (i,x,y) for (int j= (x); j>=y;j++)#defineMAXN 1000005#defineRandom (x) (rand ()% (x))using namespacestd;intn,a[maxn],k;int Select(intLintR) { if(L==R) {returna[l];} intMid=random (r-l+1)+l; intKey=a[mid];//mid for the final subscript, constantly retrieving, until the left side is smaller than key, the right is bigger than key intFirst=l,last =R; while(first<Last ) { while(last>mid&&a[last]>=key) { Last--; } swap (A[mid],a[last]); Mid=last;//at this point the last finger is smaller than mid and can be exchanged with mid and last while(first<mid&&a[first]<=key) { First++; } swap (A[first],a[mid]); Mid=first;//at this point the number of first refers to the larger than mid, ditto } if(K==mid)returnA[mid]; Else if(K<mid)return Select(l,mid-1);//There is a mistake here because it is too weak to write return ... This is an int function ... It was careless of me not to write a word that would result in a large wild number being returned Else if(mid+1<=R)return Select(mid+1, R); }intMainintargcChar Const*argv[]) {Ios::sync_with_stdio (false); CIN>>n>>K; Siji (i,1, N) cin>>A[i]; Srand ((int) Time (0));//using time as a random number seedcout<<Select(1, N); return 0;}
Dalian 42,036 Summer Camp day1-t3 (Quick select&linear Select)