File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+
3
+ #define ELEMENT_COUNT 10000
4
+
5
+ using namespace std ;
6
+
7
+ int n, k, d[ELEMENT_COUNT];
8
+
9
+ int partition (int l, int r)
10
+ {
11
+ int x = d[r], j = l - 1 ;
12
+ for (int i = l; i <= r; i++)
13
+ {
14
+ if (d[i] <= x)
15
+ {
16
+ j++;
17
+ int temp = d[i];
18
+ d[i] = d[j];
19
+ d[j] = temp;
20
+ }
21
+ }
22
+ return j + 1 ;
23
+ }
24
+
25
+ int select (int k)
26
+ {
27
+ int l = 0 , r = n - 1 ;
28
+ while (l < r)
29
+ {
30
+ int ord = partition (l, r);
31
+ if (ord < k)
32
+ {
33
+ l = ord;
34
+ }
35
+ else if (ord > k)
36
+ {
37
+ r = ord - 2 ;
38
+ }
39
+ else
40
+ {
41
+ return d[ord - 1 ];
42
+ }
43
+ }
44
+ return d[l];
45
+ }
46
+
47
+ int main ()
48
+ {
49
+ scanf (" %d%d" , &n, &k);
50
+ for (int i = 0 ; i < n; i++)
51
+ {
52
+ scanf (" %d" , &d[i]);
53
+ }
54
+ int kth = select (k);
55
+ printf (" %d" , kth);
56
+ return 0 ;
57
+ }
You can’t perform that action at this time.
0 commit comments