#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int wine[300000];
int randomized_partition(int low, int high) {
int i, temp;
i = rand() % ((high - low) + 1) + low;
temp = wine[low];
wine[low] = wine[i];
wine[i] = temp;
return partition(low, high);
}
int partition(int low, int high) {
int i, j, pivotitem, temp;
pivotitem = wine[low];
j = low;
for (i = low + 1; i <= high; i++) {
if (wine[i] <= pivotitem) {
j++;
temp = wine[i];
wine[i] = wine[j];
wine[j] = temp;
}
}
temp = wine[low];
wine[low] = wine[j];
wine[j] = temp;
return j;
}
void quicksort(int low, int high) {
int p;
if (high > low) {
p = randomized_partition(low, high);
quicksort(low, p - 1);
quicksort(p + 1, high);
}
}
int main() {
int N, K, i, first = 0, end = 1;
long long int flavor = 0;
scanf("%d %d", &N, &K);
for (i = 0; i < N; i++)
scanf("%d", &wine[i]);
//srand(tiem(NULL));
quicksort(0, N-1);
for (i = 0; i < K; i++) {
if (i == 0) {
flavor = wine[N - end];
end++;
continue;
}
if (i % 2 == 0) {
flavor += wine[N - end] - wine[0 + first];
end++;
first++;
}
}
printf("%lld", flavor);
return 0;
}
Compilation message
wine.c: In function 'randomized_partition':
wine.c:12:9: warning: implicit declaration of function 'partition' [-Wimplicit-function-declaration]
return partition(low, high);
^
wine.c: In function 'main':
wine.c:43:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &K);
^
wine.c:45:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &wine[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2292 KB |
Output is correct |
2 |
Correct |
0 ms |
2292 KB |
Output is correct |
3 |
Correct |
0 ms |
2292 KB |
Output is correct |
4 |
Correct |
0 ms |
2292 KB |
Output is correct |
5 |
Correct |
0 ms |
2292 KB |
Output is correct |
6 |
Correct |
0 ms |
2292 KB |
Output is correct |
7 |
Correct |
0 ms |
2292 KB |
Output is correct |
8 |
Correct |
0 ms |
2292 KB |
Output is correct |
9 |
Correct |
0 ms |
2292 KB |
Output is correct |
10 |
Correct |
0 ms |
2292 KB |
Output is correct |
11 |
Correct |
0 ms |
2292 KB |
Output is correct |
12 |
Correct |
0 ms |
2292 KB |
Output is correct |
13 |
Correct |
0 ms |
2292 KB |
Output is correct |
14 |
Correct |
0 ms |
2292 KB |
Output is correct |
15 |
Correct |
16 ms |
2324 KB |
Output is correct |
16 |
Correct |
0 ms |
2292 KB |
Output is correct |
17 |
Correct |
566 ms |
3100 KB |
Output is correct |
18 |
Correct |
609 ms |
3104 KB |
Output is correct |
19 |
Correct |
6 ms |
2292 KB |
Output is correct |
20 |
Correct |
6 ms |
2292 KB |
Output is correct |
21 |
Execution timed out |
1000 ms |
2704 KB |
Execution timed out |
22 |
Halted |
0 ms |
0 KB |
- |