-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path29.cpp
More file actions
58 lines (54 loc) · 1.04 KB
/
29.cpp
File metadata and controls
58 lines (54 loc) · 1.04 KB
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
#include <iostream>
#include <vector>
using namespace std;
int partition(vector<int>& a, int left, int right)
{
int pivot = a[left];
while (left < right)
{
while (left < right&&a[right] >= pivot)
right--;
a[left] = a[right];
while (left < right&&a[left] <= pivot)
left++;
a[right] = a[left];
}
a[left] = pivot;
return left;
}
vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
{
vector<int> v;
int len = input.size();
if (len == 0 || k > len || k <= 0) return v;
int left = 0, right = len - 1;
int pos = partition(input, left, right);
while (pos != k - 1)
{
if (pos > k - 1)
right = pos - 1;
else
left = pos + 1;
pos = partition(input, left, right);
}
for (int i = 0; i < k; i++)
v.push_back(input[i]);
return v;
}
int main()
{
ios::sync_with_stdio(false);
vector<int> input, v;
int n, k, temp;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> temp;
input.push_back(temp);
}
v = GetLeastNumbers_Solution(input, k);
for (int i = 0; i < k; i++)
cout << v[i] << ',';
cout << endl;
return 0;
}