×

# WA in CARVANS

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<iostream>

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\n",count);
count=0;
}


}

163811
accept rate: 0%

 1 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 ) answered 27 Dec '12, 11:17 12.4k●47●107●171 accept rate: 12% 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. (27 Dec '12, 13:53)
 1 You are doing just a little mistake. update your code by some following changes and than submit. for(int i=0;i

Here's the final code I used

# include<iostream>

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\n",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.
}
}


163811
accept rate: 0%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×3,710
×1,901
×9

question asked: 27 Dec '12, 01:22

question was seen: 941 times

last updated: 27 Dec '12, 13:58