I am trying my hands on problem "Cooling Pies" sectioned under Easy Problems. I have devised an algorithm which I believe is correct. Here are the provided test cases. 2 3 10 30 20 30 10 20 5 9 7 16 4 8 8 3 14 10 10
Now, according to me, If I sort pies weights and rack weight limits in decreasing order, then only those pies can not be cooled whose weight is greater than the first element in the rack weight array, as first element in the rack weight array would be maximum and thus, chef can not use any rack to cool a pie having weight greater than it.
After sorting,
16 9 8 7 4 (pie weights)
14 10 10 8 3 (rack limits)
Now, Since, There is only one pie whose weight is greater than first element of rack limits, it can not be cooled. However, this logic didn't served and I was responded with wrong answer.
Modifying just a bit in that algo, Now I count only pies whose weight is either less than or equal to first element of rack limits, but still I am being responded in wrong answer.
Please have a look at my code and tell me my mistake.
# include <stdio.h>
# define BUFFER 32678
static char input[BUFFER];
static int pieWt[32];
static int rack[32];
int byteRead = BUFFER;
int readInt()
{
while(byteRead < BUFFER && input[byteRead] < '0')
byteRead++;
if(byteRead == BUFFER)
{
fread(input, sizeof(char), BUFFER, stdin);
byteRead = 0;
while(byteRead < BUFFER && input[byteRead] < '0')
byteRead++;
}
int num =0;
while(byteRead < BUFFER && input[byteRead] >= '0')
{
num = num*10 + (input[byteRead++]-'0');
}
if(byteRead == BUFFER)
{
fread(input, sizeof(char), BUFFER, stdin);
byteRead = 0;
while(byteRead < BUFFER && input[byteRead] >='0')
{
num = num*10 +( input[byteRead++] - '0');
}
}
return num;
}
void bubbleSort(int*arr, int max)
{
int i, j, count=0, temp;
for(i=1;i<=max;i++)
{
count = 0;
for(j=1; j<max;j++)
{
if(arr[j] < arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
count++;
}
}
if(count==0)
break;
}
}
int main()
{
int cases, pies, i, count=0;
cases = readInt();
while(cases>0)
{
count=0;
pies = readInt();
for(i=1; i<=pies; i++)
{
pieWt[i] = readInt();
}
bubbleSort(pieWt, pies);
for(i=1; i<= pies; i++)
{
rack[i] = readInt();
}
bubbleSort(rack, pies);
for(i=1; i<=pies;i++)
{
if(pieWt[i]<=rack[1])
count++;
}
printf("%d\n", count);
cases--;
}
return 0;
}
I have another query, as I am writing readInt() function over and over again in each of the problems, I have a very strong feeling that instead of providing fast I/O, readInt() slows down my program ? Is it true because scanf() can replace readInt() in this context ?