MSTICK Algorithm

http://www.codechef.com/problems/MSTICK in this qstn i am getting WA

plz point the mistake in my algorithm that i approched

1)created a segment tree which give minimum value in given range
2)similarly created a segment tree which give maximum value in given range
3)Now let the given L and R given input for query(here i am takeing single query i.e. q=1)

a=min(L,R)//a is minimum value in range L and R(search_segment_min(0,n-1,L,R,0))

a1=max(L,R)//a1 is maximum value in range L and R(search_segment_max(0,n-1,L,R,0))

b=max(0,L-1)//b is maximum value in range L and R(search_segment_max(0,n-1,0,L-1,0))

c=max(R+1,n-1)//c is maximum value in range L and R(search_segment_max(0,n-1,R+1,n-1,0))

b1=maximum(b,c)//give maximum value btwn b,c

if(b1>(a1-a)/2)
print(b1+a);
else
print((a1+a)/2);

my code http://www.codechef.com/viewsolution/4809431

correct the floating point error. Simple trick for this problem is in two ways:
i] either add 0.01 to ur final answer nd den print upto 1 decimal place
ii] explictly print .5 if ur double is odd else print .0 if your double is even

1 Like