1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| const int maxn = 1e5 + 10; int a[maxn], n = 0, K;
inline bool invalid(int L, int R, int K) { return n == 0 || K > (R-L+1) || L > R; }
int BFPRT(int L, int R, int K); int getPivot(int L, int R) { if (R-L+1 <= 5) { sort(a+L, a+R+1); return (L+R)>>1; }
int p = L-1; // move median to the leftmost for (int i = L; i + 4 <= R; i += 5) { sort(a+i, a+i+5); swap(a[++p], a[(i + i+4)>>1]); }
return BFPRT(L, p, ((p-L+1)>>1) + 1); } int _part(int L, int R, int pivot) { // put a[pivot] -> a[R] swap(a[pivot], a[R]);
int p = L; for (int i = L; i < R; i++) if (a[i] < a[R]) { swap(a[p++], a[i]); }
swap(a[p], a[R]); return p; }
int BFPRT(int L, int R, int K) { if (invalid(L, R, K)) return -1; if (L == R) return L;
// get pivot int pivot = getPivot(L, R); int p = _part(L, R, pivot); int num = p-L+1;
if (num == K) return p; else if (num > K) return BFPRT(L, p-1, K); else return BFPRT(p+1, R, K-num); }
void init() { n = 0; }
int main() { freopen("input.txt", "r", stdin); init();
// get data scanf("%d", &n); _for(i, 0, n) scanf("%d", &a[i]); scanf("%d", &K); printf("Origin array:\n"); _for(i, 0, n) printf("%d ", a[i]);
// BFPRT int ans = BFPRT(0, n-1, K); printf("\n\n%dth number is %d\n", K, a[ans]);
printf("\nFinal array:\n"); _for(i, 0, n) printf("%d ", a[i]); }
|