I am doing this problem on SPOJ:
My kid's kindergarten class is putting up a Christmas play. (I hope he gets the lead role.) The kids are all excited, but the teacher has a lot of work. She has to produce costumes for a scene with \$K\$ soldiers. She wants to buy all the costumes in the same size, allowing for some small amount of length alteration to be done by the kids' parents later. So she has taken all the kids' height measurements. Can you help her select \$K\$ kids from her class of \$N\$ to play the soldier role, such that the height difference between the tallest and shortest in the group is minimized, and alternations will be easiest? Tell her what this minimum difference is.
INPUT
The first line contains the number of test cases \$T\$. \$T\$ test cases follow each containing 2 lines.
The first line of each test case contains 2 integers \$N\$ and \$K\$. The second line contains \$N\$ integers denoting the height of the \$N\$ kids.
OUTPUT
Output \$T\$ lines, each line containing the required answer for the corresponding test case.
CONSTRAINTS
- \$T \le 30\$
- \$1 \le K \le N \le 20000\$
- \$1 \le \text{height} \le 1000000000\$
My approach
I am first storing all the heights in the array and then sorting the array using quicksort (I also tried std::sort
), and after that finding the minimum difference using sliding window (I do not exactly know the name of algorithm, I heard it is known as sliding window).
After optimizing my code to the best that I can, my code takes 0.05s to execute all test cases, but when I see the best result, it is 0.00s or 0.02s, so less than 0.05s.
By the way, I had also tested with std::sort
but it was only marginally faster (0.05 s instead of 0.06 s). How can I optimize it more?
#include <cstdio>
#include <algorithm>
#include<iostream>
int partition(int *arr, const int left, const int right) {
const int mid = left + (right - left) / 2;
const int pivot = arr[mid];
// move the mid point value to the front.
std::swap(arr[mid],arr[left]);
int i = left + 1;
int j = right;
while (i <= j) {
while(i <= j && arr[i] <= pivot) {
i++;
}
while(i <= j && arr[j] > pivot) {
j--;
}
if (i < j) {
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i - 1],arr[left]);
return i - 1;
}
void quicksort(int *arr, const int left, const int right, const int sz)
{
if (left >= right) {
return;
}
int part = partition(arr, left, right);
quicksort(arr, left, part - 1, sz);
quicksort(arr, part + 1, right, sz);
}
using namespace std;
int main(){
int T,N,K,h[20000];
scanf("%d",&T);
while(T--){
scanf("%d %d",&N,&K);
for(int i = 0;i < N;++i) scanf("%d",&h[i]);
quicksort(h,0,N-1,N);
int ans = h[K - 1] - h[0];
for(int i = 1;i + K - 1 < N;++i)
ans = min(ans,h[i + K - 1] - h[i]);
printf("%d\n",ans);
}
return 0;
}
0.04
usingstd::sort
andstd::cin/std::cout
. Conclusion: Micro optimizations are not the solution. You should be looking for algorithmic improvements. Node:std::ios_base::sync_with_stdio(false);std::cin.tie(nullptr);
makes the stream library as fast as the C IO code. \$\endgroup\$