 # WA in CARVANS

#1

I used the following code for the problem CARVANS(http://www.codechef.com/problems/CARVANS/).I tried all the sample cases and for big inputs tried my own cases and get correct answers but the judge gives WA please help.

#include
int main()
{
int cases,cars,count=1;
long speed,tmp=0;
scanf("%d",&cases);
for(int i=0;i<cases;i++)
{
scanf("%d",&cars);
for(int j=0;j<cars;j++)
{
scanf("%ld",&speed);
if(speed<=tmp)
{
count+=1;
}
tmp=speed;
}
printf("%d
",count);
count=0;
}
}

#2

You are doing just a little mistake.

update your code by some following changes and than submit.

``````for(int i=0;i<cases;i++)
{
scanf("%u",&cars);
int speed,i,min,count = 1;

scanf("%d",&speed);
min = speed;   //minimum must be your first cars speed.

for(i=1;i<n;i++)  // than compare from second car.
{
scanf("%d",&speed);
if(speed < min) {
count++;
min = speed;
}
}
printf("%d
``````

",count);
}

please let me know if there will be any problem.

Happy Coding

#3

Upendra1234 has already corrected your code .
In your code the assignment “tmp=speed” should be within the if block .
Otherwise the logic becomes wrong .
Try the following input . The expected answer is 1 , but your program answer 2 .
Test Case :
1 ( number of test cases )
3 ( number of cars )
1 5 2 ( speed of cars )

#4

Here’s the final code I used

#include
int main()
{
int cases,cars,count=0;
long speed,tmp=2147483647;//Upper limit of signed long
scanf("%d",&cases);
for(int i=0;i<cases;i++)
{
scanf("%d",&cars);
for(int j=0;j<cars;j++)
{
scanf("%ld",&speed);
if(speed<=tmp)//Main Logic
{
count+=1;
tmp=speed;
}
}
printf("%d
",count);
count=0;
tmp=2147483647;//count and tmp are again initialized to their starting values to wipe out any new values for correct working of next cases.
}
}

#5

Thanx a lot sir actually I did not understand the question quite well but your test case cleared my doubts.I used a different code and got correct answer.

#6

Your code works fine but for the test case mentioned by vineet paliwal it gives wrong answer.