https://www.codechef.com/viewsolution/32082681
i don’t understand in which case it’s giving a wrong answer.
if(d[i]!=f[i])
fl += abs(d[i]-f[i]);
if(i != q-1)
fl += abs(d[i]-f[i+1]);
if f[i] and d[i] are equal then also you have to add abs(d[i]-f[i+1])
Since the constraint states the value of fi & di can go upto 10^6, int would cause an overflow.
Also like @sebastian pointed out the initial if condition holds only for current from and destination floors, and not for current destination and next from.
Modified your code and it works fine: CodeChef: Practical coding for everyone
Yeah I forgot to say that
t=int(input())
for i in range(t):
l=[]
m=[]
n,q=map(int,input().split())
for j in range(q):
f,d=map(int,input().split())
l.append(f)
m.append(d)
a=l[0]
for j in range(q):
if(m[j]>=l[j]):
k=m[j]-l[j]
else:
k=l[j]-m[j]
if(j!=q-1):
g = m[j]-l[j+1]
a=a+k+g
print(a)
Why am i getting wrong answer?
Its running correctly in my pycharm.
when you are adding g you are not taking absolute value
You can solve the question by using just one array.
[CodeChef: Practical coding for everyone]
you can have a look at my solution.(CodeChef: Practical coding for everyone)
I got it bro, Thanks for the help ,really this reduced my frustration