-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_max_candiestobebrought.c
86 lines (79 loc) · 1.48 KB
/
min_max_candiestobebrought.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//http://ide.geeksforgeeks.org/TpKMP2
#include<stdio.h>
#include<stdlib.h>
void quicksort(int A[],int low,int high);
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int A[], int low, int high)
{
int pivot = A[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (A[j] <= pivot)
{
i++;
swap(&A[i], &A[j]);
}
}
swap(&A[i + 1], &A[high]);
return (i + 1);
}
void quicksort(int A[], int low, int high)
{
if (low < high)
{
int pi = partition(A, low, high);
quicksort(A, low, pi - 1);
quicksort(A, pi + 1, high);
}
}
int min(int A[],int k,int n)
{
int i=0,j=n-1,count=0;
quicksort(A, 0, n-1);
while(i<j)
{
count=count+A[i];
i++;
j=j-k;
}
if(i==j)
count=count+A[i];
return count;
}
int max(int A[],int k,int n)
{
int i=0,j=n-1,count=0;
quicksort(A, 0, n-1);
while(i<j)
{
count=count+A[j];
j--;
i=i+k;
}
if(i==j)
count=count+A[j];
return count;
}
int main()
{
int n,i,j,t,k;
scanf("%d",&t);
while(t)
{
scanf("%d %d",&n,&k);
int A[n];
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
printf("%d %d\n",min(A,k,n),max(A,k,n));
t--;
}
return 0;
}