Guest User

Untitled

a guest
Jan 23rd, 2010
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. Question 1 Rearranging cartons
  2. Your school has ordered some equipment that has been delivered in a number of very heavy
  3. cartons. Each carton has a serial number and the cartons are all lined up in a row. Unfortunately,
  4. your teacher asked for the cartons to be placed in a particular sequence and you
  5. forgot to tell the people who unloaded the cartons about this. You now have to quickly
  6. restore the cartons to the correct order before the teacher comes and sees how you have
  7. messed up her instructions.
  8. Since the cartons are very heavy, you cannot carry them over long distances. In each
  9. step, all you can do is to exchange the position of two adjacent cartons.
  10. For instance, suppose the serial numbers on the cartons in the order in which they are
  11. unloaded are 34, 29, 12, 78 and 90 and the order in which the cartons were supposed to be
  12. arranged is 90, 29, 78, 34, 12. These cartons can be rearranged in the desired order with 7
  13. exchanges, as follows:
  14. � Exchange 78, 90 � 34, 29, 12, 90, 78
  15. � Exchange 12, 90 � 34, 29, 90, 12, 78
  16. � Exchange 34, 29 � 29, 34, 12, 90, 78
  17. � Exchange 12, 78 � 29, 34, 90, 78, 12
  18. � Exchange 34, 90 � 29, 90, 34, 78, 12
  19. � Exchange 29, 90 � 90, 29, 34, 78, 12
  20. � Exchange 34, 78 � 90, 29, 78, 34, 12
  21. In this example, it can be shown that 7 exchanges are needed to reorder the cartons as
  22. desired.
  23. Clearly, you want to get the job done with minimum effort. Given the initial arrangement
  24. of the cartons and the final sequence that the teacher wants, your goal is to compute the
  25. minimum number of exchanges required to rearrange the cartons in the desired order.
  26. Input format
  27. The first line of input is a single integer N, the total number of cartons. The second line
  28. consists of N distinct positive integers, separated by spaces, denoting the serial numbers of
  29. the N cartons in the order in which they were unloaded. The third line is another sequence
  30. of N integers, denoting the desired order in which the N cartons should be rearranged. The
  31. sequence of numbers in the third line is guaranteed to be a permutation of the sequence in
  32. the second line.
  33. Output format
  34. The output should be a single integer, the minimum number of exchanges required to achieve
  35. the desired sequence of cartons.
  36. 1
  37. Test data
  38. In 90% of the inputs, 1  N  6000. In 10% of the inputs, 1  N  100000.
  39. Example
  40. Here is the sample input and output corresponding to the example discussed above.
  41. Sample input
  42. 5
  43. 34 29 12 78 90
  44. 90 29 78 34 12
  45. Sample output
  46. 7
  47.  
Advertisement
Add Comment
Please, Sign In to add comment