CARVANS - Editorial

“”“This approach works too ,and is faster than other appraoches mentioned above”""
“”“AC in one go”""

test=int(raw_input())
for _ in range(test):
N=int(raw_input())
count=1
arr=list()
arr=[int(i) for i in raw_input().split()]
for i in range(len(arr)-1):
	if(arr[i+1]>arr[i]):
		arr[i+1]=arr[i]
	else:
		count+=1
print count

can some one find mistake in this code

import java.util.;
import java.lang.
;
import java.io.*;
class klf
{
public static void main(String[]args) throws Exception
{
Scanner cin=new Scanner(System.in);
Stackst;
int t=cin.nextInt();
while(t–>0)
{
st=new Stack();
int n=cin.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=cin.nextInt();
}
int count=0;
for(int i=0;i<n;i++)
{
if(st.empty())
{
st.push(a[i]);
count++;
}
else if(a[i]<st.peek())
{
st.push(a[i]);
count++;
}
else
{
st.push(a[i]);
}
}
while(!st.empty())
st.pop();
System.out.println(count);
}
}
}

strong text
Can anybody please let me know what is wrong with my code…
strong text
#include<stdio.h>

int main(){
int rounds,cars,speed,count=0;
for(scanf("%d",&rounds);rounds;rounds–){
int min=10000;
count=0;
for(scanf("%d",&cars);cars;cars–){
scanf("%d",&speed);
if(speed<=min){
count++;
min=speed;
}
}
printf("%d\n",count);

}

return 0;

}

Does Input to this problem is in file?
Am i need to take input from files?

Just have a look : https://www.codechef.com/viewsolution/22546307,
You will not need an array!

1 Like

the obvious/funny thing is :since at anytime we are dealing with two consecutive cars only, problem can be solved without the need of an array.

why this shows time limit exceed?

http://www.codechef.com/viewsolution/1370908

Use scanf, printf instead of cin, cout.

2 Likes

By the way, why 4MB is 4,000,000,000 bytes :slight_smile:

It is mentioned in three problems CARVANS, PPERM and COALSCAM.

if W==1 you still need to input speed of the only car of this test. Otherwise you consider its speed as a number of cars for the next test. Also while(–N) as well as while(–W) is wrong. By this you miss the last test case, last car respectively. Use while(N–) and while(W–) instead.

1 Like

@anton_lunyov …you’re right, corrected those.

Got a correct one !
thanks

Did you even run the program on local machine and test? its full of errors!

printf("1\n");    // this was using front slash
while(--W)        // --W and not W-- this was causing TLE
printf("%d\n",counter); // reference was passed here!

Sorry. This was my advise. Second while really should have --W. I didn’t see the scanf before :slight_smile:

Yes. All three of us missed that :stuck_out_tongue: Will fix it for the problems in the practice section.

The third car also moves with speed 4 since it can not overtake the second car. So yes, we should compare only consecutive cars but the speed of each car may change.

what is wrong in this question

please help me

1 Like

@yellow_agony @fushar : kindly update the test-cases, as the code with normal string comparisons is also giving AC, e.g. the cars : [4 5 50 20 2 1] gives output as 4 as cars at maximum speed are [4 20 2 1], whereas ideally it should give output as 3 as cars at maximum speeds are [4 2 1]. So the code with string comparison should not run as the speed is integral/numerical comparison. Correct me if I am wrong

Why am I getting WA in this code?
https://www.codechef.com/viewsolution/23882065

t = int(input())

for _ in range(t):
n = int(input())
if(n != 0):
count = 1
else:
count = 0
speed_of_cars = []
speed_of_cars = [int(x) for x in input().split()]

for i in range(n-1):
	if(speed_of_cars[i+1] < speed_of_cars[i]):
		count += 1

print(count)

There is no need for array at all,
run a loop and keep track of min value as you read the input.
And the answer is how many times you read values less than or equal to current min. Thats It.