-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMOOC4_1.cpp
More file actions
34 lines (33 loc) · 860 Bytes
/
MOOC4_1.cpp
File metadata and controls
34 lines (33 loc) · 860 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void arrange(const int s, const int e, vector<int> &elems, const int k)
{
int pi = s, pj = e - 1;
while(pi != pj)
{
while(pi < pj && elems[pi] < elems[pj])
pj--;
swap(elems[pi], elems[pj]);
while(pi < pj && elems[pi] < elems[pj])
pi++;
swap(elems[pi], elems[pj]);
}
if(pi == e - k)return;
if(pi > e - k)arrange(s, pi, elems, k - e + pi);
if(pi < e - k)arrange(pi + 1, e, elems, k);
}
int main()
{
int n, k;
cin >> n;
vector<int>elems(n);
for(int i = 0; i < n; i++)
cin >> elems[i];
cin >> k;
arrange(0, n, elems, k);
sort(elems.end() - k, elems.end());
for(int i = n - 1; i >= n - k; i--)
cout<<elems[i]<<endl;
}