see, I am explaining with the help of first test case of problem . there are six word in input . i store their index with the help of map.
now , when we take second input of words , i simply one by one call update function and count those pair which changed relative position . how it count ?
suppose i encounter ith index word . then first we update it's index value in array a. then i count how many word with index less than or equal to its index already exist in array before encounter it.
there are two case :-
1. the number of words which encounter before ith word exactly equal to its index value . that's means there is no change in relative position of ith word with these words. so
ith index - count(ith index) = 0 .
- the number of words which encounter before ith word less then its index value that's means some words will encounter after ith word . the words which will not encounter yet , the relative position of these some word will be changed with ith word . the no of these word = ith index - count(ith index ) > 0 .
like that we iterate for all word and sum up all values which come in second case.
for first test case ;- initially index values are 1 2 3 4 5 6
after changed position of words their index 5 6 4 1 2 3.
for first word no of changed relative order pair = (5 - 1) = 4 .
for 2nd word '' '' = (6-2) = 4.
for 3rd word = (4 - 1) = 3.
for 4th word = (1-1) = 0.
for 5th word = (2-2) = 0.
for 6th word = (3-3) = 0;
total pair = 4+4+3 = 11 which is answer.
i wrote first time on any such type forum . so sorry for lengthy explanation .