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.
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
Yes. All three of us missed that 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
@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.
Wrong answer
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ll;
#define mod 1000000007
int main()
{
int t,n,min,prev_car_speed,i,count,min_index;
cin >> t;
while(t–)
{
cin >> n;
min = 999999999;
count = 1;
ll a[n];
for(i=0; i<n; i++)
{
cin >> a[i];
if(a[i] < min)
min = a[i];
min_index = i;
}
prev_car_speed = a[0];
for(i=1; i<=min_index; i++)
{
if(a[i] <=prev_car_speed)
{
count++;
prev_car_speed = a[i];
}
else
{
prev_car_speed = a[i];
}
}
cout << count << "\n";
}
}
Can anyone plz tell me why my submission is not displaying the correct answer?
https://www.codechef.com/viewsolution/25944363
if the test is 4 5 1 2 3, then the total cars moving with max speed are four(4 1 2 3) right? Then why is 2 the answer, can someone please explain.
@guruch2206
Basic idea of algorithm - Develop a new array B in which value of ith index is min(A[i],A[i-1])
A= [4, 5, 1, 2, 3]
B= [4, 4, 1, 1, 2]
A representing the maximum speed of each and B representing the actual speed they will be travelling
Now , as we can see only cars with speed 4 and 1 are actually travelling at their maximum speed hence the count is 2
what should be the output for test case 7 6 9 5 10 8 4 3
6 or 5
hi thanks for the reply
But it is not showing time limit exceeded even with the first solution. Here is my code
https://www.codechef.com/viewsolution/31701382
I am facing the same problem please can someone tell whats the problem here
5