given an array, count the first k large numbers and output the k numbers from large to small.
enter
the first line contains an integer n that represents the size of the array. N < 100000. The second line of
contains n integers, representing the elements of the array, separated by a space. The absolute value of each integer does not exceed 100000000. The third line of
contains an integer k. K < n.
output
output the first k large numbers from large to small, one line each.
sample input
10
4 56 9 8 7 1 2 30
5
sample output
9
8
7
6
5
my code is as follows
using namespace std;
int a [100010];
void swap (int & a, int & b) / / Exchange variable a, b value
{
int tmp = a;
a = b;
b = tmp;
}
void print (int a [], int s, int e) {/ / output array s to e items
for (int i = 1; i < (e - s)+1; iPP)
cout << a[s + i] <<endl;
}
void arrange (int a [], int s, int epenint k) {
if (s >= e) return;
int m= a[s];
int i = s, j = e;
while (i != j) {
while (j > i&&a[j] >= m) { //mm
--j;
}
swap(a[i], a[j]);
while (j > i&&a[i] <= m)
PPi;
swap(a[i], a[j]);
}
if ((e - j) == k) print(a, j, e); //mk
if ((e - j) > k) arrange(a, j, e, k); //karrange
if ((e - j) < k) arrange(a, s, j, e-j - k); //karrange
}
int main () {
int n;
cin >> n;
for (int i = 0; i < n; iPP)
cin >> a[i];
int k;
cin >> k;
arrange(a, 0, n - 1, k);
return 0;
}
sample output is correct, but the online submission is wrong, and it should be timed out. Maybe big data my program will not work, ask for advice