You are not logged in. Please login at www.codechef.com to post your questions!

×

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;
}

}

asked 27 Dec '12, 01:22

shikhs123's gravatar image

3★shikhs123
163811
accept rate: 0%


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 )

link

answered 27 Dec '12, 11:17

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
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) shikhs1233★

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

please let me know if there will be any problem.

Happy Coding

link

answered 27 Dec '12, 09:27

upendra1234's gravatar image

2★upendra1234
2.3k183069
accept rate: 1%

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

(27 Dec '12, 13:54) shikhs1233★

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.
}
}
link

answered 27 Dec '12, 13:58

shikhs123's gravatar image

3★shikhs123
163811
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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